1
1
<?php
2
+
2
3
/**
3
4
* Library of static methods for manipulating arrays.
5
+ *
4
6
* @package framework
5
7
* @subpackage misc
6
8
*/
@@ -43,7 +45,10 @@ class ArrayLib {
43
45
* @return array
44
46
*/
45
47
public static function invert ($ arr ) {
46
- if (!$ arr ) return false ;
48
+ if (!$ arr ) {
49
+ return false ;
50
+ }
51
+
47
52
$ result = array ();
48
53
49
54
foreach ($ arr as $ columnName => $ column ) {
@@ -56,7 +61,7 @@ public static function invert($arr) {
56
61
}
57
62
58
63
/**
59
- * Return an array where the keys are all equal to the values
64
+ * Return an array where the keys are all equal to the values.
60
65
*
61
66
* @param $arr array
62
67
* @return array
@@ -70,7 +75,8 @@ public static function valuekey($arr) {
70
75
*/
71
76
public static function array_values_recursive ($ arr ) {
72
77
$ lst = array ();
73
- foreach (array_keys ($ arr ) as $ k ){
78
+
79
+ foreach (array_keys ($ arr ) as $ k ) {
74
80
$ v = $ arr [$ k ];
75
81
if (is_scalar ($ v )) {
76
82
$ lst [] = $ v ;
@@ -80,92 +86,115 @@ public static function array_values_recursive($arr) {
80
86
);
81
87
}
82
88
}
89
+
83
90
return $ lst ;
84
91
}
85
92
86
93
/**
87
- * Filter an array by keys (useful for only allowing certain form-input to be saved).
94
+ * Filter an array by keys (useful for only allowing certain form-input to
95
+ * be saved).
88
96
*
89
97
* @param $arr array
90
98
* @param $keys array
99
+ *
91
100
* @return array
92
101
*/
93
- public static function filter_keys ($ arr , $ keys )
94
- {
95
- foreach ($ arr as $ key => $ v ) {
96
- if (!in_array ($ key , $ keys )) {
102
+ public static function filter_keys ($ arr , $ keys ) {
103
+ foreach ($ arr as $ key => $ v ) {
104
+ if (!in_array ($ key , $ keys )) {
97
105
unset($ arr [$ key ]);
98
106
}
99
107
}
108
+
100
109
return $ arr ;
101
110
}
102
111
103
112
/**
104
- * Determines if an array is associative by checking
105
- * for existing keys via array_key_exists().
113
+ * Determines if an array is associative by checking for existing keys via
114
+ * array_key_exists().
115
+ *
106
116
* @see http://nz.php.net/manual/en/function.is-array.php#76188
107
117
*
108
118
* @param array $arr
119
+ *
109
120
* @return boolean
110
121
*/
111
122
public static function is_associative ($ arr ) {
112
123
if (is_array ($ arr ) && ! empty ($ arr )) {
113
124
for ($ iterator = count ($ arr ) - 1 ; $ iterator ; $ iterator --) {
114
- if (!array_key_exists ($ iterator , $ arr )) return true ;
125
+ if (!array_key_exists ($ iterator , $ arr )) {
126
+ return true ;
127
+ }
115
128
}
129
+
116
130
return !array_key_exists (0 , $ arr );
117
131
}
132
+
118
133
return false ;
119
134
}
120
135
121
136
/**
122
137
* Recursively searches an array $haystack for the value(s) $needle.
138
+ *
123
139
* Assumes that all values in $needle (if $needle is an array) are at
124
140
* the SAME level, not spread across multiple dimensions of the $haystack.
125
141
*
126
142
* @param mixed $needle
127
143
* @param array $haystack
128
144
* @param boolean $strict
145
+ *
129
146
* @return boolean
130
147
*/
131
148
public static function in_array_recursive ($ needle , $ haystack , $ strict = false ) {
132
- if (!is_array ($ haystack )) return false ; // Not an array, we've gone as far as we can down this branch
133
-
134
- if (in_array ($ needle , $ haystack , $ strict )) return true ; // Is it in this level of the array?
135
- else {
136
- foreach ($ haystack as $ obj ) { // It's not, loop over the rest of this array
137
- if (self ::in_array_recursive ($ needle , $ obj , $ strict )) return true ;
149
+ if (!is_array ($ haystack )) {
150
+ return false ;
151
+ }
152
+
153
+ if (in_array ($ needle , $ haystack , $ strict )) {
154
+ return true ;
155
+ } else {
156
+ foreach ($ haystack as $ obj ) {
157
+ if (self ::in_array_recursive ($ needle , $ obj , $ strict )) {
158
+ return true ;
159
+ }
138
160
}
139
161
}
140
162
141
- return false ; // Never found $needle :(
163
+ return false ;
142
164
}
143
165
144
166
/**
145
167
* Recursively merges two or more arrays.
146
168
*
147
- * Behaves similar to array_merge_recursive(), however it only merges values when both are arrays
148
- * rather than creating a new array with both values, as the PHP version does. The same behaviour
149
- * also occurs with numeric keys, to match that of what PHP does to generate $_REQUEST.
169
+ * Behaves similar to array_merge_recursive(), however it only merges
170
+ * values when both are arrays rather than creating a new array with
171
+ * both values, as the PHP version does. The same behaviour also occurs
172
+ * with numeric keys, to match that of what PHP does to generate $_REQUEST.
173
+ *
174
+ * @param array $array
150
175
*
151
- * @param array $array, ...
152
176
* @return array
153
177
*/
154
178
public static function array_merge_recursive ($ array ) {
155
179
$ arrays = func_get_args ();
156
180
$ merged = array ();
181
+
157
182
if (count ($ arrays ) == 1 ) {
158
183
return $ array ;
159
184
}
185
+
160
186
while ($ arrays ) {
161
187
$ array = array_shift ($ arrays );
188
+
162
189
if (!is_array ($ array )) {
163
190
trigger_error ('ArrayLib::array_merge_recursive() encountered a non array argument ' , E_USER_WARNING );
164
191
return ;
165
192
}
193
+
166
194
if (!$ array ) {
167
195
continue ;
168
196
}
197
+
169
198
foreach ($ array as $ key => $ value ) {
170
199
if (is_array ($ value ) && array_key_exists ($ key , $ merged ) && is_array ($ merged [$ key ])) {
171
200
$ merged [$ key ] = ArrayLib::array_merge_recursive ($ merged [$ key ], $ value );
@@ -174,6 +203,30 @@ public static function array_merge_recursive($array) {
174
203
}
175
204
}
176
205
}
206
+
177
207
return $ merged ;
178
208
}
209
+
210
+ /**
211
+ * Takes an multi dimension array and returns the flattened version.
212
+ *
213
+ * @param array $array
214
+ * @param boolean $preserveKeys
215
+ *
216
+ * @return array
217
+ */
218
+ public static function flatten ($ array , $ preserveKeys = true , &$ out = array ()) {
219
+ foreach ($ array as $ key => $ child ) {
220
+ if (is_array ($ child )) {
221
+ $ out = self ::flatten ($ child , $ preserveKeys , $ out );
222
+ } else if ($ preserveKeys ) {
223
+ $ out [$ key ] = $ child ;
224
+ } else {
225
+ $ out [] = $ child ;
226
+ }
227
+ }
228
+
229
+ return $ out ;
230
+ }
179
231
}
232
+
0 commit comments