|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * Given an array, find all the values recursively. |
5 |
| - * |
6 |
| - * @param array $array The Array to be Flattened |
7 |
| - * @param bool $allow_duplicates Should the array allow duplicates |
8 |
| - * @return array The resulting array or NULL on failure |
9 |
| - */ |
10 |
| -function array_flatten( array $array, $allow_duplicates = false ) { |
11 |
| - $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); |
12 |
| - $final = array(); |
13 |
| - foreach( $it as $v ) { |
14 |
| - if( $allow_duplicates ) { |
15 |
| - $final[] = $v; |
16 |
| - } else { |
17 |
| - $final[$v] = $v; |
| 3 | +if( !function_exists('array_flatten') ) { |
| 4 | + /** |
| 5 | + * Given an array, find all the values recursively. |
| 6 | + * |
| 7 | + * @param array $array The Array to be Flattened |
| 8 | + * @param bool $allow_duplicates Should the array allow duplicates |
| 9 | + * @return array The resulting array or NULL on failure |
| 10 | + */ |
| 11 | + function array_flatten( array $array, $allow_duplicates = false ) { |
| 12 | + $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); |
| 13 | + $final = array(); |
| 14 | + foreach( $it as $v ) { |
| 15 | + if( $allow_duplicates ) { |
| 16 | + $final[] = $v; |
| 17 | + } else { |
| 18 | + $final[$v] = $v; |
| 19 | + } |
18 | 20 | }
|
19 |
| - } |
20 | 21 |
|
21 |
| - return $final; |
| 22 | + return $final; |
| 23 | + } |
22 | 24 | }
|
23 | 25 |
|
24 |
| -/** |
25 |
| - * Given an array of arrays, merges the array's children together. |
26 |
| - * |
27 |
| - * @param array $arrays An array of arrays. |
28 |
| - * @param array|null $keys The merged array. |
29 |
| - * @return array |
30 |
| - */ |
31 |
| -function array_blend( array $arrays, array $keys = null ) { |
32 |
| - if( !is_array($arrays) ) return array(); |
33 |
| - $out = array(); |
| 26 | +if( !function_exists('array_blend') ) { |
| 27 | + /** |
| 28 | + * Given an array of arrays, merges the array's children together. |
| 29 | + * |
| 30 | + * @param array $arrays An array of arrays. |
| 31 | + * @param array|null $keys The merged array. |
| 32 | + * @return array The resulting blended array |
| 33 | + */ |
| 34 | + function array_blend( array $arrays, array $keys = null ) { |
| 35 | + if( !is_array($arrays) ) return array(); |
| 36 | + $out = array(); |
34 | 37 |
|
35 |
| - foreach( $arrays as $key => $array ) { |
36 |
| - if( is_array($array) && (is_null($keys) || (in_array($key, $keys))) ) { |
37 |
| - foreach( $array as $value ) { |
38 |
| - $out [] = $value; |
| 38 | + foreach( $arrays as $key => $array ) { |
| 39 | + if( is_array($array) && (is_null($keys) || (in_array($key, $keys))) ) { |
| 40 | + foreach( $array as $value ) { |
| 41 | + $out [] = $value; |
| 42 | + } |
39 | 43 | }
|
40 | 44 | }
|
41 |
| - } |
42 | 45 |
|
43 |
| - return $out; |
| 46 | + return $out; |
| 47 | + } |
44 | 48 | }
|
45 | 49 |
|
46 |
| -/** |
47 |
| - * Given an array of similarly keyed arrays, returns an array of only the values of the key. |
48 |
| - * |
49 |
| - * @param array $arrays An array of similarly keyed arrays |
50 |
| - * @param int|string $key the desired key |
51 |
| - * @return array the flattened array |
52 |
| - */ |
53 |
| -function array_key_array( array $arrays, $key ) { |
54 |
| - if( !is_array($arrays) ) { |
55 |
| - return array(); |
56 |
| - } |
57 |
| - $out = array(); |
58 |
| - foreach( $arrays as $i => $array ) { |
59 |
| - $out[$i] = $array[$key]; |
60 |
| - } |
| 50 | +if( !function_exists('array_key_array') ) { |
| 51 | + /** |
| 52 | + * Given an array of similarly keyed arrays, returns an array of only the values of the key. |
| 53 | + * |
| 54 | + * @param array $arrays An array of similarly keyed arrays |
| 55 | + * @param int|string $key The desired key |
| 56 | + * @return array The flattened array |
| 57 | + */ |
| 58 | + function array_key_array( array $arrays, $key ) { |
| 59 | + if( !is_array($arrays) ) { |
| 60 | + return array(); |
| 61 | + } |
| 62 | + $out = array(); |
| 63 | + foreach( $arrays as $i => $array ) { |
| 64 | + $out[$i] = $array[$key]; |
| 65 | + } |
61 | 66 |
|
62 |
| - return $out; |
| 67 | + return $out; |
| 68 | + } |
63 | 69 | }
|
64 | 70 |
|
65 |
| -/** |
66 |
| - * Given an array of similarly keyed arrays, returns an array of only the selected keys. |
67 |
| - * |
68 |
| - * @param array $arrays |
69 |
| - * @param array|int|string $keys The key or array of keys to return |
70 |
| - * @return array |
71 |
| - */ |
72 |
| -function array_keys_array( array $arrays, $keys ) { |
73 |
| - $keys = (array)$keys; |
| 71 | +if( !function_exists('array_blend') ) { |
| 72 | + /** |
| 73 | + * Given an array of similarly keyed arrays, returns an array of only the selected keys. |
| 74 | + * |
| 75 | + * @param array $arrays An array of similarly keyed arrays |
| 76 | + * @param array|int|string $keys The key or array of keys to return |
| 77 | + * @return array The array of arrays with just the selected keys |
| 78 | + */ |
| 79 | + function array_keys_array( array $arrays, $keys ) { |
| 80 | + $keys = (array)$keys; |
74 | 81 |
|
75 |
| - $out = array(); |
76 |
| - foreach( $arrays as $i => $array ) { |
77 |
| - foreach( $keys as $index ) { |
78 |
| - $out[$i][$index] = $array[$index]; |
| 82 | + $out = array(); |
| 83 | + foreach( $arrays as $i => $array ) { |
| 84 | + foreach( $keys as $index ) { |
| 85 | + $out[$i][$index] = $array[$index]; |
| 86 | + } |
79 | 87 | }
|
80 |
| - } |
81 | 88 |
|
82 |
| - return $out; |
| 89 | + return $out; |
| 90 | + } |
83 | 91 | }
|
84 | 92 |
|
85 |
| -/** |
86 |
| - * Given a keyed array, fills any missing values. |
87 |
| - * |
88 |
| - * @param array $array A Keyed array |
89 |
| - * @param array $keys The keys that must exist |
90 |
| - * @param mixed $fill The desired value to fill with |
91 |
| - * @return array |
92 |
| - */ |
93 |
| -function array_key_refill( array $array, $keys, $fill = array() ) { |
94 |
| - foreach( $keys as $key ) { |
95 |
| - if( !isset($array[$key]) ) { |
96 |
| - $array[$key] = $fill; |
| 93 | +if( !function_exists('array_key_refill') ) { |
| 94 | + /** |
| 95 | + * Given a keyed array, fills any missing values. |
| 96 | + * |
| 97 | + * @param array $array A Keyed array |
| 98 | + * @param array $keys The keys that must exist |
| 99 | + * @param mixed $fill The desired value to fill with |
| 100 | + * @return array |
| 101 | + */ |
| 102 | + function array_key_refill( array $array, $keys, $fill = array() ) { |
| 103 | + foreach( $keys as $key ) { |
| 104 | + if( !isset($array[$key]) ) { |
| 105 | + $array[$key] = $fill; |
| 106 | + } |
97 | 107 | }
|
98 |
| - } |
99 | 108 |
|
100 |
| - return $array; |
| 109 | + return $array; |
| 110 | + } |
101 | 111 | }
|
102 |
| - |
|
0 commit comments