Asked  7 Months ago    Answers:  5   Viewed   32 times

I've got an array of cats objects:

$cats = Array
        [0] => stdClass Object
                [id] => 15
        [1] => stdClass Object
                [id] => 18
        [2] => stdClass Object
                [id] => 23

and I want to extract an array of cats' IDs in 1 line (not a function nor a loop).

I was thinking about using array_walk with create_function but I don't know how to do it.

Any idea?



If you have PHP 5.5 or later, the best way is to use the built in function array_column():

$idCats = array_column($cats, 'id');

But the son has to be an array or converted to an array

Wednesday, March 31, 2021
answered 7 Months ago

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario:

Related usability ticket:

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
answered 7 Months ago

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
answered 5 Months ago

You either iterate the array, searching for the particular record (ok in a one time only search) or build a hashmap using another associative array.

For the former, something like this

$item = null;
foreach($array as $struct) {
    if ($v == $struct->ID) {
        $item = $struct;

See this question and subsequent answers for more information on the latter - Reference PHP array by multiple indexes

Saturday, June 5, 2021
answered 5 Months ago

You can use reduce and check if the checked property is true, then push (As pointed out by assoron) the value to the accumulator - there is no need for 2 loops:

const arr = [
  { "value": "abc", "checked": true },
  { "value": "xyz", "checked": false },
  { "value": "lmn", "checked": true }

const filtered = arr.reduce((a, o) => (o.checked && a.push(o.value), a), [])      
Wednesday, August 11, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :