I have an array of class objects:
class Foo
{
public $A;
public $B;
public $C;
}
I need a new array of C fields. Is there a way to convert the array without explicit loops? Hate that after C#.
// Explicit conversion:
foreach ($arr as $item)
{
$Cs[] = $item->C;
}
Regards,
$Cs = array_map(function($item) {
return $item->C;
}, $arr);
I believe you can use get_object_vars
.
$arr = get_object_vars(new Foo());
var_dump($arr); // should give Array(3) { "A"=>NULL, "B"=>NULL, "C"=>NULL }