1 | <?php |
||
9 | class ArrayLib { |
||
10 | |||
11 | /** |
||
12 | * Inverses the first and second level keys of an associative |
||
13 | * array, keying the result by the second level, and combines |
||
14 | * all first level entries within them. |
||
15 | * |
||
16 | * Before: |
||
17 | * <example> |
||
18 | * array( |
||
19 | * 'row1' => array( |
||
20 | * 'col1' =>'val1', |
||
21 | * 'col2' => 'val2' |
||
22 | * ), |
||
23 | * 'row2' => array( |
||
24 | * 'col1' => 'val3', |
||
25 | * 'col2' => 'val4' |
||
26 | * ) |
||
27 | * ) |
||
28 | * </example> |
||
29 | * |
||
30 | * After: |
||
31 | * <example> |
||
32 | * array( |
||
33 | * 'col1' => array( |
||
34 | * 'row1' => 'val1', |
||
35 | * 'row2' => 'val3', |
||
36 | * ), |
||
37 | * 'col2' => array( |
||
38 | * 'row1' => 'val2', |
||
39 | * 'row2' => 'val4', |
||
40 | * ), |
||
41 | * ) |
||
42 | * </example> |
||
43 | * |
||
44 | * @param array $arr |
||
45 | * @return array |
||
46 | */ |
||
47 | public static function invert($arr) { |
||
62 | |||
63 | /** |
||
64 | * Return an array where the keys are all equal to the values. |
||
65 | * |
||
66 | * @param $arr array |
||
67 | * @return array |
||
68 | */ |
||
69 | public static function valuekey($arr) { |
||
72 | |||
73 | /** |
||
74 | * @todo Improve documentation |
||
75 | */ |
||
76 | public static function array_values_recursive($arr) { |
||
92 | |||
93 | /** |
||
94 | * Filter an array by keys (useful for only allowing certain form-input to |
||
95 | * be saved). |
||
96 | * |
||
97 | * @param $arr array |
||
98 | * @param $keys array |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | public static function filter_keys($arr, $keys) { |
||
111 | |||
112 | /** |
||
113 | * Determines if an array is associative by checking for existing keys via |
||
114 | * array_key_exists(). |
||
115 | * |
||
116 | * @see http://nz.php.net/manual/en/function.is-array.php#76188 |
||
117 | * |
||
118 | * @param array $arr |
||
119 | * |
||
120 | * @return boolean |
||
121 | */ |
||
122 | public static function is_associative($arr) { |
||
135 | |||
136 | /** |
||
137 | * Recursively searches an array $haystack for the value(s) $needle. |
||
138 | * |
||
139 | * Assumes that all values in $needle (if $needle is an array) are at |
||
140 | * the SAME level, not spread across multiple dimensions of the $haystack. |
||
141 | * |
||
142 | * @param mixed $needle |
||
143 | * @param array $haystack |
||
144 | * @param boolean $strict |
||
145 | * |
||
146 | * @return boolean |
||
147 | */ |
||
148 | public static function in_array_recursive($needle, $haystack, $strict = false) { |
||
165 | |||
166 | /** |
||
167 | * Similar to array_map, but recurses when arrays are encountered. |
||
168 | * |
||
169 | * Actually only one array argument is supported. |
||
170 | * |
||
171 | * @param $f callback to apply |
||
172 | * @param $array array |
||
173 | * @return array |
||
174 | */ |
||
175 | public static function array_map_recursive($f, $array) { |
||
182 | |||
183 | /** |
||
184 | * Recursively merges two or more arrays. |
||
185 | * |
||
186 | * Behaves similar to array_merge_recursive(), however it only merges |
||
187 | * values when both are arrays rather than creating a new array with |
||
188 | * both values, as the PHP version does. The same behaviour also occurs |
||
189 | * with numeric keys, to match that of what PHP does to generate $_REQUEST. |
||
190 | * |
||
191 | * @param array $array |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | public static function array_merge_recursive($array) { |
||
226 | |||
227 | /** |
||
228 | * Takes an multi dimension array and returns the flattened version. |
||
229 | * |
||
230 | * @param array $array |
||
231 | * @param boolean $preserveKeys |
||
232 | * @param array $out |
||
233 | * |
||
234 | * @return array |
||
235 | */ |
||
236 | public static function flatten($array, $preserveKeys = true, &$out = array()) { |
||
249 | } |
||
250 | |||
251 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.