|
| 1 | +<?php |
| 2 | + |
| 3 | +if (!function_exists('array_column')) { |
| 4 | + /** |
| 5 | + * This file is part of the array_column library |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) Ben Ramsey (http://benramsey.com) |
| 11 | + * @license http://opensource.org/licenses/MIT MIT |
| 12 | + */ |
| 13 | + |
| 14 | + /** |
| 15 | + * Returns the values from a single column of the input array, identified by |
| 16 | + * the $columnKey. |
| 17 | + * |
| 18 | + * Optionally, you may provide an $indexKey to index the values in the returned |
| 19 | + * array by the values from the $indexKey column in the input array. |
| 20 | + * |
| 21 | + * @param array $input A multi-dimensional array (record set) from which to pull |
| 22 | + * a column of values. |
| 23 | + * @param mixed $columnKey The column of values to return. This value may be the |
| 24 | + * integer key of the column you wish to retrieve, or it |
| 25 | + * may be the string key name for an associative array. |
| 26 | + * @param mixed $indexKey (Optional.) The column to use as the index/keys for |
| 27 | + * the returned array. This value may be the integer key |
| 28 | + * of the column, or it may be the string key name. |
| 29 | + * @return array |
| 30 | + */ |
| 31 | + function array_column($input = null, $columnKey = null, $indexKey = null) |
| 32 | + { |
| 33 | + // Using func_get_args() in order to check for proper number of |
| 34 | + // parameters and trigger errors exactly as the built-in array_column() |
| 35 | + // does in PHP 5.5. |
| 36 | + $argc = func_num_args(); |
| 37 | + $params = func_get_args(); |
| 38 | + |
| 39 | + if ($argc < 2) { |
| 40 | + trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING); |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + if (!is_array($params[0])) { |
| 45 | + trigger_error( |
| 46 | + 'array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', |
| 47 | + E_USER_WARNING |
| 48 | + ); |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + if (!is_int($params[1]) |
| 53 | + && !is_float($params[1]) |
| 54 | + && !is_string($params[1]) |
| 55 | + && $params[1] !== null |
| 56 | + && !(is_object($params[1]) && method_exists($params[1], '__toString')) |
| 57 | + ) { |
| 58 | + trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING); |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + if (isset($params[2]) |
| 63 | + && !is_int($params[2]) |
| 64 | + && !is_float($params[2]) |
| 65 | + && !is_string($params[2]) |
| 66 | + && !(is_object($params[2]) && method_exists($params[2], '__toString')) |
| 67 | + ) { |
| 68 | + trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING); |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + $paramsInput = $params[0]; |
| 73 | + $paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null; |
| 74 | + |
| 75 | + $paramsIndexKey = null; |
| 76 | + if (isset($params[2])) { |
| 77 | + if (is_float($params[2]) || is_int($params[2])) { |
| 78 | + $paramsIndexKey = (int) $params[2]; |
| 79 | + } else { |
| 80 | + $paramsIndexKey = (string) $params[2]; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + $resultArray = array(); |
| 85 | + |
| 86 | + foreach ($paramsInput as $row) { |
| 87 | + $key = $value = null; |
| 88 | + $keySet = $valueSet = false; |
| 89 | + |
| 90 | + if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) { |
| 91 | + $keySet = true; |
| 92 | + $key = (string) $row[$paramsIndexKey]; |
| 93 | + } |
| 94 | + |
| 95 | + if ($paramsColumnKey === null) { |
| 96 | + $valueSet = true; |
| 97 | + $value = $row; |
| 98 | + } elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) { |
| 99 | + $valueSet = true; |
| 100 | + $value = $row[$paramsColumnKey]; |
| 101 | + } |
| 102 | + |
| 103 | + if ($valueSet) { |
| 104 | + if ($keySet) { |
| 105 | + $resultArray[$key] = $value; |
| 106 | + } else { |
| 107 | + $resultArray[] = $value; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + return $resultArray; |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments