note 118831 added to function.array-column
| From: | Anonymous | Date: | Fri, 12 Feb 2016 16:45:46 +0000 |
| Subject: | note 118831 added to function.array-column | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204929@lists.php.net to get a copy of this message | ||
I added a little more functionality to the more popular answers here to support the $index_key
parameter for PHP < 5.5
<?php
// for php < 5.5
if (!function_exists('array_column')) {
function array_column($input, $column_key, $index_key = null) {
$arr = array_map(function($d) use ($column_key, $index_key) {
if (!isset($d[$column_key])) {
return null;
}
if ($index_key !== null) {
return array($d[$index_key] => $d[$column_key]);
}
return $d[$column_key];
}, $input);
if ($index_key !== null) {
$tmp = array();
foreach ($arr as $ar) {
$tmp[key($ar)] = current($ar);
}
$arr = $tmp;
}
return $arr;
}
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 74.204.99.226
----
Manual Page -- http://php.net/manual/en/function.array-column.php
Edit -- https://master.php.net/note/edit/118831
Del: integrated -- https://master.php.net/note/delete/118831/integrated
Del: useless -- https://master.php.net/note/delete/118831/useless
Del: bad code -- https://master.php.net/note/delete/118831/bad+code
Del: spam -- https://master.php.net/note/delete/118831/spam
Del: non-english -- https://master.php.net/note/delete/118831/non-english
Del: in docs -- https://master.php.net/note/delete/118831/in+docs
Del: other reasons-- https://master.php.net/note/delete/118831
Reject -- https://master.php.net/note/reject/118831
Search -- https://master.php.net/manage/user-notes.php