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?
This question is related to
php
Warning
create_function()
has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.
Builtin loops in PHP are faster then interpreted loops, so it actually makes sense to make this one a one-liner:
$result = array();
array_walk($cats, create_function('$value, $key, &$result', '$result[] = $value->id;'), $result)
$object = new stdClass();
$object->id = 1;
$object2 = new stdClass();
$object2->id = 2;
$objects = [
$object,
$object2
];
$ids = array_map(function ($object) {
/** @var YourEntity $object */
return $object->id;
// Or even if you have public methods
// return $object->getId()
}, $objects);
Output: [1, 2]
// $array that contain records and id is what we want to fetch a
$ids = array_column($array, 'id');
Warning
create_function()
has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.
You can use the array_map()
function.
This should do it:
$catIds = array_map(create_function('$o', 'return $o->id;'), $objects);
As @Relequestual writes below, the function is now integrated directly in the array_map. The new version of the solution looks like this:
$catIds = array_map(function($o) { return $o->id;}, $objects);
You can do it easily with ouzo goodies
$result = array_map(Functions::extract()->id, $arr);
or with Arrays (from ouzo goodies)
$result = Arrays::map($arr, Functions::extract()->id);
Check out: http://ouzo.readthedocs.org/en/latest/utils/functions.html#extract
See also functional programming with ouzo (I cannot post a link).
The create_function()
function is deprecated as of php v7.2.0. You can use the array_map()
as given,
function getObjectID($obj){
return $obj->id;
}
$IDs = array_map('getObjectID' , $array_of_object);
Alternatively, you can use array_column()
function which returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array. You can use the array_column as given,
$IDs = array_column($array_of_object , 'id');
function extract_ids($cats){
$res = array();
foreach($cats as $k=>$v) {
$res[]= $v->id;
}
return $res
}
and use it in one line:
$ids = extract_ids($cats);
CODE
<?php
# setup test array.
$cats = array();
$cats[] = (object) array('id' => 15);
$cats[] = (object) array('id' => 18);
$cats[] = (object) array('id' => 23);
function extract_ids($array = array())
{
$ids = array();
foreach ($array as $object) {
$ids[] = $object->id;
}
return $ids;
}
$cat_ids = extract_ids($cats);
var_dump($cats);
var_dump($cat_ids);
?>
OUTPUT
# var_dump($cats);
array(3) {
[0]=>
object(stdClass)#1 (1) {
["id"]=>
int(15)
}
[1]=>
object(stdClass)#2 (1) {
["id"]=>
int(18)
}
[2]=>
object(stdClass)#3 (1) {
["id"]=>
int(23)
}
}
# var_dump($cat_ids);
array(3) {
[0]=>
int(15)
[1]=>
int(18)
[2]=>
int(23)
}
I know its using a loop, but it's the simplest way to do it! And using a function it still ends up on a single line.
The solution depends on the PHP version you are using. At least there are 2 solutions:
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 \stdClass
es as used in the question it is only possible with PHP versions >= 7.0
. But when using an array
containing array
s you can do the same since PHP >= 5.5
.
@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 Closure
s, like followed:
$catIds = array_map(function($o) { return $o->id; }, $objects);
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:
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
...
while (true)
{
$objects = array_map(function($o) { return $o->id; }, $objects);
echo memory_get_usage() . "\n";
sleep(1);
}
// the output
4235136
4235168
4235168
4235168
...
Memory leak?! Is Garbage Collector doing right when using 'create_function' within 'array_map'?
Source: Stackoverflow.com