@ 2013-06-23T04:36:17Z <?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'
)
);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 5.4.16 0.017 0.060 12.40 5.4.15 0.018 0.042 12.40 5.4.14 0.017 0.051 12.09 5.4.13 0.018 0.042 12.07 5.4.12 0.019 0.048 12.04 5.4.11 0.027 0.070 12.03 5.4.10 0.017 0.043 12.03 5.4.9 0.015 0.042 12.03 5.4.8 0.016 0.042 12.03 5.4.7 0.016 0.041 12.03 5.4.6 0.021 0.047 12.03 5.4.5 0.015 0.043 12.02 5.4.4 0.015 0.045 12.01 5.4.3 0.014 0.046 12.01 5.4.2 0.014 0.043 12.00 5.4.1 0.017 0.041 12.01 5.4.0 0.015 0.043 11.50 5.3.26 0.013 0.050 12.72 5.3.25 0.002 0.044 12.72 5.3.24 0.014 0.046 12.72 5.3.23 0.020 0.048 12.71 5.3.22 0.023 0.056 12.68 5.3.21 0.014 0.046 12.68 5.3.20 0.017 0.045 12.68 5.3.19 0.018 0.043 12.68 5.3.18 0.016 0.043 12.67 5.3.17 0.015 0.044 12.67 5.3.16 0.018 0.051 12.67 5.3.15 0.019 0.051 12.67 5.3.14 0.019 0.040 12.66 5.3.13 0.019 0.042 12.66 5.3.12 0.015 0.048 12.66 5.3.11 0.018 0.043 12.66 5.3.10 0.016 0.044 12.14 5.3.9 0.015 0.045 12.12 5.3.8 0.016 0.054 12.10 5.3.7 0.016 0.043 12.10 5.3.6 0.014 0.045 12.09 5.3.5 0.023 0.059 12.04 5.3.4 0.023 0.058 12.04 5.3.3 0.013 0.044 12.00 5.3.2 0.014 0.043 11.78 5.3.1 0.018 0.038 11.75 5.3.0 0.020 0.047 11.73
preferences:dark mode live preview
140.57 ms | 1386 KiB | 7 Q