3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function array_column1($input = null, $columnKey = null, $indexKey = null) { 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($input)) { trigger_error('array_column() expects parameter 1 to be array, ' . gettype($input) . ' given', E_USER_WARNING); return null; } if (null !== $columnKey && !is_int($columnKey) && !is_string($columnKey) && !(is_object($columnKey) && method_exists($columnKey, '__toString')) ) { trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING); return false; } if (isset($indexKey) && !is_int($indexKey) && !is_string($indexKey) && !(is_object($indexKey) && method_exists($indexKey, '__toString')) ) { trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING); return false; } $resultArray = array(); foreach ($input as $row) { $key = $value = null; $keySet = $valueSet = false; if ($indexKey !== null && array_key_exists($indexKey, $row)) { $keySet = true; $key = $row[$indexKey]; } if (is_array($row)) { if (null === $columnKey) { $valueSet = true; $value = $row; } elseif (array_key_exists($columnKey, $row)) { $valueSet = true; $value = $row[$columnKey]; } } if ($valueSet) { if ($keySet) { $resultArray[$key] = $value; } else { $resultArray[] = $value; } } } return $resultArray; } var_dump( array_column1( array( array( 'a' => null, 'b' => 16, ), ), 'a' ) );
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(1) { [0]=> NULL }