1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | // ------------------------------------------------------------------------ |
||
12 | /** |
||
13 | * Array Helper |
||
14 | * |
||
15 | * A collection of helper function to work with array. |
||
16 | */ |
||
17 | // ------------------------------------------------------------------------ |
||
18 | if ( ! function_exists('array_get_value')) { |
||
19 | /** |
||
20 | * array_get_value |
||
21 | * |
||
22 | * Lets you determine whether an array index is set and whether it has a value. |
||
23 | * If the element is empty it returns NULL (or whatever you specify as the default value). |
||
24 | * |
||
25 | * @param string $key |
||
26 | * @param array $array |
||
27 | * @param mixed $default |
||
28 | * |
||
29 | * @return mixed |
||
30 | */ |
||
31 | function array_get_value($key, array $array, $default = null) |
||
32 | { |
||
33 | return array_key_exists($key, $array) ? $array[ $key ] : $default; |
||
34 | } |
||
35 | } |
||
36 | |||
37 | // ------------------------------------------------------------------------ |
||
38 | |||
39 | if ( ! function_exists('array_get_values')) { |
||
40 | /** |
||
41 | * array_get_values |
||
42 | * |
||
43 | * Returns only the array items specified. Will return a default value if |
||
44 | * it is not set. |
||
45 | * |
||
46 | * @param array $keys |
||
47 | * @param array $array |
||
48 | * @param array $default |
||
49 | * |
||
50 | * @return array |
||
51 | */ |
||
52 | function array_get_values(array $keys, array $array, $default = []) |
||
53 | { |
||
54 | $return = []; |
||
55 | |||
56 | is_array($keys) OR $keys = [$keys]; |
||
57 | |||
58 | foreach ($keys as $item) { |
||
59 | if (array_key_exists($item, $array)) { |
||
60 | $return[ $item ] = $array[ $item ]; |
||
61 | } elseif (is_array($default) && array_key_exists($item, $default)) { |
||
62 | $return[ $item ] = $default[ $item ]; |
||
63 | } elseif ( ! empty($default)) { |
||
64 | $return[ $item ] = $default; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | return $return; |
||
69 | |||
70 | } |
||
71 | } |
||
72 | |||
73 | // ------------------------------------------------------------------------ |
||
74 | |||
75 | if ( ! function_exists('array_combines')) { |
||
76 | /** |
||
77 | * array_combines |
||
78 | * |
||
79 | * Creates an array by using one array for keys and another for its values and keep all the values. |
||
80 | * |
||
81 | * @param array $keys |
||
82 | * @param array $values |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | function array_combines(array $keys, array $values) |
||
87 | { |
||
88 | $combine_array = []; |
||
89 | |||
90 | foreach ($keys as $index => $key) { |
||
91 | $combine_array[ $key ][] = $values[ $index ]; |
||
92 | } |
||
93 | |||
94 | array_walk( |
||
95 | $combine_array, |
||
96 | function (&$value) { |
||
97 | $value = (count($value) == 1) ? array_pop($value) : $value; |
||
98 | } |
||
99 | ); |
||
100 | |||
101 | return $combine_array; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | // ------------------------------------------------------------------------ |
||
106 | |||
107 | if ( ! function_exists('array_group')) { |
||
108 | /** |
||
109 | * array_group |
||
110 | * |
||
111 | * Group the array by value or key. |
||
112 | * |
||
113 | * @param array $array |
||
114 | * @param bool $flip |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | function array_group(array $array, $flip = true) |
||
119 | { |
||
120 | $group_array = []; |
||
121 | |||
122 | if ($flip) { |
||
123 | array_walk_recursive( |
||
124 | $array, |
||
125 | function ($value, $key) use (&$group_array) { |
||
126 | if ( ! isset($group_array[ $value ]) || ! is_array($group_array[ $value ])) { |
||
127 | $group_array[ $value ] = []; |
||
128 | } |
||
129 | $group_array[ $value ][] = $key; |
||
130 | } |
||
131 | ); |
||
132 | } else { |
||
133 | array_walk_recursive( |
||
134 | $array, |
||
135 | function ($value, $key) use (&$group_array) { |
||
136 | $group_array[ $key ][] = $value; |
||
137 | } |
||
138 | ); |
||
139 | } |
||
140 | |||
141 | return $group_array; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | // ------------------------------------------------------------------------ |
||
146 | |||
147 | if ( ! function_exists('array_filter_recursive')) { |
||
148 | /** |
||
149 | * array_filter_recursive |
||
150 | * |
||
151 | * Remove element by the value of array. |
||
152 | * |
||
153 | * @param array $array The array source. |
||
154 | * @param mixed $value |
||
155 | * @param int $limit |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | function array_filter_recursive(&$array, $value, $limit = 0) |
||
160 | { |
||
161 | if (is_array($value)) { |
||
162 | foreach ($value as $remove) { |
||
163 | $array = array_filter_recursive($array, $remove, $limit); |
||
164 | } |
||
165 | |||
166 | return $array; |
||
167 | } |
||
168 | |||
169 | $result = []; |
||
170 | $count = 0; |
||
171 | |||
172 | foreach ($array as $key => $value) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
173 | if ($count > 0 and $count == $limit) { |
||
174 | return $result; |
||
175 | } |
||
176 | if ( ! is_array($value)) { |
||
177 | if ($key != $value) { |
||
178 | $result[ $key ] = $value; |
||
179 | $count++; |
||
180 | } |
||
181 | } else { |
||
182 | $sub = array_filter_recursive($value, $value, $limit); |
||
183 | if (count($sub) > 0) { |
||
184 | if ($key != $value) { |
||
185 | $result[ $key ] = $sub; |
||
186 | $count += count($sub); |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | |||
192 | return $result; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | // ------------------------------------------------------------------------ |
||
197 | |||
198 | if ( ! function_exists('array_search_recursive')) { |
||
199 | /** |
||
200 | * array_search_recursive |
||
201 | * |
||
202 | * Searches the multidimensional array for a given value and returns the first corresponding key if successful. |
||
203 | * |
||
204 | * @param mixed $needle The searched value. |
||
205 | * @param array $haystack The multidimensional array. |
||
206 | * @param bool $strict |
||
207 | * |
||
208 | * @return bool|int|string |
||
209 | */ |
||
210 | function array_search_recursive($needle, array $haystack, $strict = false) |
||
211 | { |
||
212 | $result = ''; |
||
213 | foreach ($haystack as $key => $value) { |
||
214 | if ($strict === false) { |
||
215 | $needle = strtolower($needle); |
||
216 | |||
217 | if ( ! is_array($value)) { |
||
218 | $value = strtolower($value); |
||
219 | } else { |
||
220 | $value = array_map('strtolower', $value); |
||
221 | } |
||
222 | } |
||
223 | |||
224 | if ($needle === $value) { |
||
225 | $result = $key; |
||
226 | break; |
||
227 | } elseif (is_array($value)) { |
||
228 | if (array_search_recursive($needle, $value)) { |
||
229 | $result = $key; |
||
230 | break; |
||
231 | } |
||
232 | } |
||
233 | } |
||
234 | |||
235 | return ($result == '') ? false : $result; |
||
236 | } |
||
237 | } |
||
238 | |||
239 | // ------------------------------------------------------------------------ |
||
240 | |||
241 | if ( ! function_exists('array_unique_recursive')) { |
||
242 | /** |
||
243 | * array_unique_recursive |
||
244 | * |
||
245 | * Removes duplicate values from an multidimensional array. |
||
246 | * |
||
247 | * @param array $array Array Source |
||
248 | * |
||
249 | * @return array |
||
250 | */ |
||
251 | function array_unique_recursive(array $array) |
||
252 | { |
||
253 | $serialized = array_map('serialize', $array); |
||
254 | $unique = array_unique($serialized); |
||
255 | |||
256 | return array_intersect_key($array, $unique); |
||
257 | } |
||
258 | } |
||
259 | |||
260 | // ------------------------------------------------------------------------ |
||
261 | |||
262 | if ( ! function_exists('array_flatten')) { |
||
263 | /** |
||
264 | * array_flatten |
||
265 | * |
||
266 | * Merge an multidimensional array into regular array. |
||
267 | * |
||
268 | * @param array $array |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | function array_flatten(array $array = []) |
||
273 | { |
||
274 | $flat_array = []; |
||
275 | |||
276 | foreach ($array as $key => $value) { |
||
277 | if (is_array($value)) { |
||
278 | $flat_array = array_merge($flat_array, array_flatten($value)); |
||
279 | } else { |
||
280 | $flat_array[ $key ] = $value; |
||
281 | } |
||
282 | } |
||
283 | |||
284 | return $flat_array; |
||
285 | } |
||
286 | } |
||
287 | |||
288 | // ------------------------------------------------------------------------ |
||
289 | |||
290 | if ( ! function_exists('range_price')) { |
||
291 | /** |
||
292 | * range_price |
||
293 | * |
||
294 | * Create an array containing a range of prices. |
||
295 | * |
||
296 | * @param int $min The minimum price numeric value. |
||
297 | * @param int $max The maximum price numeric value. |
||
298 | * @param int $multiplier The multiplier used in the range, default is 0. |
||
299 | * |
||
300 | * @return array |
||
301 | */ |
||
302 | function range_price($min, $max, $multiplier = 0) |
||
303 | { |
||
304 | $multiplier = $multiplier * 20; |
||
305 | $num_range = $max / $min; |
||
306 | $num_step = $multiplier / $min / 100; |
||
307 | |||
308 | $ranges = []; |
||
309 | foreach (range(0, $num_range, $num_step) as $num_price) { |
||
310 | if ($num_price == 0) { |
||
311 | $ranges[] = $min; |
||
312 | } else { |
||
313 | $ranges[] = $num_price * $min / 2 * 10; |
||
314 | } |
||
315 | } |
||
316 | |||
317 | $prices = []; |
||
318 | for ($i = 0; $i < count($ranges); $i++) { |
||
0 ignored issues
–
show
It seems like you are calling the size function
count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}
// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
![]() |
|||
319 | if ($ranges[ $i ] == $max) { |
||
320 | break; |
||
321 | } else { |
||
322 | $prices[ $ranges[ $i ] ] = ($ranges[ $i + 1 ] == 0) ? $ranges[ $i ] * 2 : $ranges[ $i + 1 ]; |
||
323 | } |
||
324 | } |
||
325 | |||
326 | return $prices; |
||
327 | } |
||
328 | } |
||
329 | |||
330 | // ------------------------------------------------------------------------ |
||
331 | |||
332 | |||
333 | if ( ! function_exists('range_date')) { |
||
334 | /** |
||
335 | * range_date |
||
336 | * |
||
337 | * Creates an array containing a range of dates. |
||
338 | * |
||
339 | * @param string|int $start_date Start Date |
||
340 | * @param int $days Num of days |
||
341 | * |
||
342 | * @return array |
||
343 | */ |
||
344 | function range_date($start_date, $days = 1) |
||
345 | { |
||
346 | $start_date = (is_string($start_date) ? strtotime($start_date) : $start_date); |
||
347 | |||
348 | $date_range = []; |
||
349 | for ($i = 0; $i < $days; $i++) { |
||
350 | $date_range[ $i ] = $start_date + ($i * 24 * 60 * 60); |
||
351 | } |
||
352 | |||
353 | return $date_range; |
||
354 | } |
||
355 | } |
||
356 | // ------------------------------------------------------------------------ |
||
357 | |||
358 | if ( ! function_exists('range_year')) { |
||
359 | /** |
||
360 | * range_year |
||
361 | * |
||
362 | * Create an array containing a range of years. |
||
363 | * |
||
364 | * @param int $min The minimum numeric year value. |
||
365 | * @param null $max The maximum numeric year value. |
||
0 ignored issues
–
show
|
|||
366 | * @param int $step The increment used in the range, default is 1. |
||
367 | * |
||
368 | * @return array |
||
369 | */ |
||
370 | function range_year($min = 1995, $max = null, $step = 1) |
||
371 | { |
||
372 | $max = empty($max) ? date('Y') : $max; |
||
373 | |||
374 | $years = []; |
||
375 | |||
376 | foreach (range($min, $max, $step) as $year) { |
||
377 | $years[ $year ] = $year; |
||
378 | } |
||
379 | |||
380 | return $years; |
||
381 | } |
||
382 | } |