This function will fail when you try to fetch columns from an indexed array.
<?php
$a =array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
print_r(array_column($a,'0'));
?>
Expected results should be :
Array
(
[0] => 1
[1] => 4
[2] => 7
)
But we get :
Array (
)
Instead I have create another function.
<?php
function my_array_column($array,$colName){
$results=array();
if(!is_array($array)) return $results;
foreach($array as $child){
if(!is_array($child)) continue;
if(array_key_exists($colName,$child)){
$results[] = $child[$colName];
}
}
return $results;
}
$a =array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
print_r(my_array_column($a,'0'));
?>
Now we get the right results:
Array
(
[0] => 1
[1] => 4
[2] => 7
)