<?php
function array_column1($input = null, $columnKey = null, $indexKey = null)
{
// Using func_get_args() in order to check for proper number of
// parameters and trigger errors exactly as the built-in array_column()
// does in PHP 5.5.
$params = func_get_args();
switch (func_num_args()) {
case 0:
trigger_error('array_column() expects at least 2 parameters, 0 given', E_USER_WARNING);
return null;
case 1:
trigger_error('array_column() expects at least 2 parameters, 1 given', E_USER_WARNING);
return null;
}
if (!is_array($params[0])) {
trigger_error('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING);
return null;
}
if (!is_int($params[1])
&& !is_string($params[1])
&& !(is_object($params[1]) && method_exists($params[1], '__toString'))
) {
trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
return false;
}
if (isset($params[2])
&& !is_int($params[2])
&& !is_string($params[2])
&& !(is_object($params[2]) && method_exists($params[2], '__toString'))
) {
trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
return false;
}
$paramsInput = $params[0];
$paramsColumnKey = (string) $params[1];
$paramsIndexKey = (isset($params[2]) ? (string) $params[2] : null);
$resultArray = array();
foreach ($paramsInput as $row) {
$key = $value = null;
$keySet = $valueSet = false;
if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
$keySet = true;
$key = $row[$paramsIndexKey];
}
if (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
$valueSet = true;
$value = $row[$paramsColumnKey];
}
if ($valueSet) {
if ($keySet) {
$resultArray[$key] = $value;
} else {
$resultArray[] = $value;
}
}
}
return $resultArray;
}
var_dump(
array_column(
array(
array(
'a' => null,
'b' => 16,
),
),
'b',
'a'
)
);
var_dump(
array_column1(
array(
array(
'a' => null,
'b' => 16,
),
),
'b',
'a'
)
);
- Output for git.master, git.master_jit, rfc.property-hooks
- array(1) {
[""]=>
int(16)
}
array(1) {
[""]=>
int(16)
}
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
33.06 ms | 401 KiB | 8 Q