|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of the array_column library |
| 4 | + * |
| 5 | + * For the full copyright and license information, please view the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + * |
| 8 | + * @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com> |
| 9 | + * @license http://opensource.org/licenses/MIT MIT |
| 10 | + */ |
| 11 | + |
| 12 | +if (!function_exists('array_column')) { |
| 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 | + $params = func_get_args(); |
| 37 | + |
| 38 | + if (!isset($params[0])) { |
| 39 | + trigger_error('array_column() expects at least 2 parameters, 0 given', E_USER_WARNING); |
| 40 | + return null; |
| 41 | + } elseif (!isset($params[1])) { |
| 42 | + trigger_error('array_column() expects at least 2 parameters, 1 given', E_USER_WARNING); |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + if (!is_array($params[0])) { |
| 47 | + trigger_error('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING); |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + if (!is_int($params[1]) |
| 52 | + && !is_string($params[1]) |
| 53 | + && !(is_object($params[1]) && method_exists($params[1], '__toString')) |
| 54 | + ) { |
| 55 | + trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING); |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + if (isset($params[2]) |
| 60 | + && !is_int($params[2]) |
| 61 | + && !is_string($params[2]) |
| 62 | + && !(is_object($params[2]) && method_exists($params[2], '__toString')) |
| 63 | + ) { |
| 64 | + trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING); |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + $paramsInput = $params[0]; |
| 69 | + $paramsColumnKey = (string) $params[1]; |
| 70 | + $paramsIndexKey = (isset($params[2]) ? (string) $params[2] : null); |
| 71 | + $resultArray = array(); |
| 72 | + |
| 73 | + foreach ($paramsInput as $row) { |
| 74 | + |
| 75 | + $key = $value = null; |
| 76 | + $keySet = $valueSet = false; |
| 77 | + |
| 78 | + if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) { |
| 79 | + $keySet = true; |
| 80 | + $key = $row[$paramsIndexKey]; |
| 81 | + } |
| 82 | + |
| 83 | + if (is_array($row) && array_key_exists($paramsColumnKey, $row)) { |
| 84 | + $valueSet = true; |
| 85 | + $value = $row[$paramsColumnKey]; |
| 86 | + } |
| 87 | + |
| 88 | + if ($valueSet) { |
| 89 | + if ($keySet) { |
| 90 | + $resultArray[$key] = $value; |
| 91 | + } else { |
| 92 | + $resultArray[] = $value; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + return $resultArray; |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments