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