note 112531 added to function.array-column
| From: | janvenekamp | Date: | Wed, 26 Jun 2013 16:00:27 +0000 |
| Subject: | note 112531 added to function.array-column | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195365@lists.php.net to get a copy of this message | ||
Implementation for PHP < 5.5.0
Following: https://wiki.php.net/rfc/array_column
<?php
// PHP < 5.5.0
if (!function_exists('array_column')) {
function array_column($input, $column_key, $index_key = null)
{
if ($index_key !== null) {
// Collect the keys
$keys = array();
$i = 0; // Counter for numerical keys when key does not exist
foreach ($input as $row) {
if (array_key_exists($index_key, $row)) {
// Update counter for numerical keys
if (is_numeric($row[$index_key]) || is_bool($row[$index_key])) {
$i = max($i, (int) $row[$index_key] + 1);
}
// Get the key from a single column of the array
$keys[] = $row[$index_key];
} else {
// The key does not exist, use numerical indexing
$keys[] = $i++;
}
}
}
if ($column_key !== null) {
// Collect the values
$values = array();
$i = 0; // Counter for removing keys
foreach ($input as $row) {
if (array_key_exists($column_key, $row)) {
// Get the values from a single column of the input array
$values[] = $row[$column_key];
$i++;
} elseif (isset($keys)) {
// Values does not exist, also drop the key for it
array_splice($keys, $i, 1);
}
}
} else {
// Get the full arrays
$values = array_values($input);
}
if ($index_key !== null) {
return array_combine($keys, $values);
}
return $values;
}
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 217.74.18.2
----
Manual Page -- http://php.net/manual/en/function.array-column.php
Edit -- https://master.php.net/note/edit/112531
Del: integrated -- https://master.php.net/note/delete/112531/integrated
Del: useless -- https://master.php.net/note/delete/112531/useless
Del: bad code -- https://master.php.net/note/delete/112531/bad+code
Del: spam -- https://master.php.net/note/delete/112531/spam
Del: non-english -- https://master.php.net/note/delete/112531/non-english
Del: in docs -- https://master.php.net/note/delete/112531/in+docs
Del: other reasons-- https://master.php.net/note/delete/112531
Reject -- https://master.php.net/note/reject/112531
Search -- https://master.php.net/manage/user-notes.php