Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
if ( !function_exist('array_column') ) {
function array_column( $collection, $field, $keyfield = null ) {
$items = array();
foreach ( $collection as $k => $item ) {
$key = $keyfield ? $item[$keyfield] : $k;
$items[ $key ] = $item[$field];
}
return $items;
}
}
/**
* Returns the values from a single column of the input array, identified by the columnKey.
*
* Optionally, you may provide an indexKey to index the values in the returned array by the values from the indexKey column in the input array.
*
* @param array[] $input A multi-dimensional array (record set) from which to pull a column of values.
* @param int|string $columnKey The column of values to return. This value may be the integer key of the column you wish to retrieve, or it may be the string key name for an associative array.
* @param int|string $indexKey The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name.
*
* @return mixed[]
*/
function array_column ($input, $columnKey, $indexKey = null) {
if (!is_array($input)) {
return false;
}
if ($indexKey === null) {
foreach ($input as $i => &$in) {
if (is_array($in) && isset($in[$columnKey])) {
$in = $in[$columnKey];
} else {
unset($input[$i]);
}
}
} else {
$result = [];
foreach ($input as $i => $in) {
if (is_array($in) && isset($in[$columnKey])) {
if (isset($in[$indexKey])) {
$result[$in[$indexKey]] = $in[$columnKey];
} else {
$result[] = $in[$columnKey];
}
unset($input[$i]);
}
}
$input = &$result;
}
return $input;
}
array_column(), добавилась возможность указать не только столбец значений (второй параметр), но и столбец ключей (необязательный третий параметр).null для его пропуска (третий параметр при отсутствии второго становится обязательным):array_column($rows, null, 'id');
array_combine(array_column($rows, 'id'), $rows);
foreach.А этот баг как раз затрагивается всех пользователей windows
Мне всегда казалось что те кто добровольно вошли в ряды мейнтенеров языка добровольно взяли не себя какие то дополнительные обязательства, например, по исправлению неинтересных багов, нет?
В PHP 5.5 будет функция array_column