Skip to content

Instantly share code, notes, and snippets.

@RJ
Created May 21, 2009 18:25
Show Gist options
  • Save RJ/115616 to your computer and use it in GitHub Desktop.
Save RJ/115616 to your computer and use it in GitHub Desktop.
<?php
function array_map_eval($code, $array)
{
$retval=array();
foreach($array as $_) $retval[]=eval($code);
return $retval;
}
$input = array();
for($i=0;$i<2;++$i)
{
$obj = new stdclass;
$obj->id = $i;
$obj->foo = "bar$i";
$input[]=$obj;
}
$output = array_map_eval('return $_->id;', $input);
print_r($input);
print_r($output);
exit;
/* OUTPUT:
Array
(
[0] => stdClass Object
(
[id] => 0
[foo] => bar0
)
[1] => stdClass Object
(
[id] => 1
[foo] => bar1
)
)
Array
(
[0] => 0
[1] => 1
)
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment