note 113942 added to function.array-column
| From: | ohcc at 163 dot com | Date: | Thu, 19 Dec 2013 13:47:52 +0000 |
| Subject: | note 113942 added to function.array-column | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-197984@lists.php.net to get a copy of this message | ||
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
)
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/function.array-column.php
Edit -- https://master.php.net/note/edit/113942
Del: integrated -- https://master.php.net/note/delete/113942/integrated
Del: useless -- https://master.php.net/note/delete/113942/useless
Del: bad code -- https://master.php.net/note/delete/113942/bad+code
Del: spam -- https://master.php.net/note/delete/113942/spam
Del: non-english -- https://master.php.net/note/delete/113942/non-english
Del: in docs -- https://master.php.net/note/delete/113942/in+docs
Del: other reasons-- https://master.php.net/note/delete/113942
Reject -- https://master.php.net/note/reject/113942
Search -- https://master.php.net/manage/user-notes.php