note 112217 deleted from function.array-column by sobak
| From: | sobak@php.net | Date: | Tue, 01 Jul 2014 17:37:57 +0000 |
| Subject: | note 112217 deleted from function.array-column by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199801@lists.php.net to get a copy of this message | ||
Note Submitter: vovan-ve at yandex dot ru
----
>>> for < PHP5.5 , following function can be used :
NEVER edit items of array argument by reference! It WILL damage items of original array, which are
referenced outside with another variables like this:
$foo = array(10, 20, 30);
$bar = &$foo[1];
You should just to collect a new array instead of changing array by reference.
So, userland implementation can be such a following:
# PHP < 5.5
function array_column(array $input, $columnKey, $indexKey = null) {
$result = array();
if (null === $indexKey) {
if (null === $columnKey) {
// trigger_error('What are you doing? Use array_values() instead!',
E_USER_NOTICE);
$result = array_values($input);
}
else {
foreach ($input as $row) {
$result[] = $row[$columnKey];
}
}
}
else {
if (null === $columnKey) {
foreach ($input as $row) {
$result[$row[$indexKey]] = $row;
}
}
else {
foreach ($input as $row) {
$result[$row[$indexKey]] = $row[$columnKey];
}
}
}
return $result;
}