The solution depends on the PHP version you are using. At least there are 2 solutions:
First (Newer PHP versions)
As @JosepAlsina said before the best and also shortest solution is to use array_column as following:
$catIds = array_column($objects, 'id');Notice: For iterating an array containing \stdClasses as used in the question it is only possible with PHP versions >= 7.0. But when using an array containing arrays you can do the same since PHP >= 5.5.
Second (Older PHP versions)
@Greg said in older PHP versions it is possible to do following:
$catIds = array_map(create_function('$o', 'return $o->id;'), $objects);But beware: In newer PHP versions >= 5.3.0 it is better to use Closures, like followed:
$catIds = array_map(function($o) { return $o->id; }, $objects);The difference
First solution creates a new function and puts it into your RAM. The garbage collector does not delete the already created and already called function instance out of memory for some reason. And that regardless of the fact, that the created function instance can never be called again, because we have no pointer for it. And the next time when this code is called, the same function will be created again. This behavior slowly fills your memory...
Both examples with memory output to compare them:
BAD
while (true) { $objects = array_map(create_function('$o', 'return $o->id;'), $objects); echo memory_get_usage() . "\n"; sleep(1); } // the output 4235616 4236600 4237560 4238520 ...GOOD
while (true) { $objects = array_map(function($o) { return $o->id; }, $objects); echo memory_get_usage() . "\n"; sleep(1); } // the output 4235136 4235168 4235168 4235168 ...This may also be discussed here
Memory leak?! Is Garbage Collector doing right when using 'create_function' within 'array_map'?
919 votes
3 answers
With PHP 7.4, I have associative array with objets :
$array = [ 1 => Class { 'id' => 1, 'call' => true, }, 5 => Class { 'id' => 5, 'call' => false, }, 7 => Class { 'id' => 7, 'call' => true, }, ]I want to extract all the IDs if thecall attribute ===true.
After searching, I think I should use thearray_map function.
$ids = array_map(function($e) { if (true === $e->call) { return $e->id; } }, $array); // Return : array(3) { [1]=> 1 [5]=> NULL [7]=> 7 }But I have two problems with this:
- I don't wantNULL results
- I don't wish to have the associative keys in in my newarray. I want a new array ([0=>1, 1=>7])
I know I can do it with aforeach() but I find it interesting to do it with a single PHP function (array_walk orarray_map ?)
2021-09-25
833
votes
Answer
Solution:
You probably wantarray_filter to filter out what you don't want, then you can extract theid:
$ids = array_column(array_filter($array, function($e) { return $e->call === true; }), 'id');Undefined answered
2021-09-25
Link to answer
97
votes
Answer
Solution:
Create an array which is thecall value indexed by theid and then filter it. As the id is the index, then extract the keys for the ids...
$ids = array_keys(array_filter(array_column($array, "call", "id"))); print_r($ids)); Array ( [0] => 1 [1] => 7 )Although I think aforeach() is a much more straight forward method.
Undefined answered
2021-09-25
Link to answer
318
votes
Answer
Solution:
You can reduce the array with a callback that conditionally adds each item's id to the "carry" array.
$ids = array_reduce($array, fn($c, $i) => $i->call ? [...$c, $i->id] : $c, []);Undefined answered
2021-09-25
Link to answer
People are also looking for solutions of the problem: property [id] does not exist on this collection instance.
Source
Share
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.