|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System PHP 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 null $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
|
|
|
if ( ! function_exists('array_filter_recursive')) { |
|
146
|
|
|
/** |
|
147
|
|
|
* array_filter_recursive |
|
148
|
|
|
* |
|
149
|
|
|
* Remove element by the value of array. |
|
150
|
|
|
* |
|
151
|
|
|
* @param array $array The array source. |
|
152
|
|
|
* @param mixed $value |
|
153
|
|
|
* @param int $limit |
|
154
|
|
|
* |
|
155
|
|
|
* @return array |
|
156
|
|
|
*/ |
|
157
|
|
|
function array_filter_recursive(&$array, $value, $limit = 0) |
|
158
|
|
|
{ |
|
159
|
|
|
if (is_array($value)) { |
|
160
|
|
|
foreach ($value as $remove) { |
|
161
|
|
|
$array = array_filter_recursive($array, $remove, $limit); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $array; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$result = []; |
|
168
|
|
|
$count = 0; |
|
169
|
|
|
|
|
170
|
|
|
foreach ($array as $key => $value) { |
|
|
|
|
|
|
171
|
|
|
if ($count > 0 and $count == $limit) { |
|
172
|
|
|
return $result; |
|
173
|
|
|
} |
|
174
|
|
|
if ( ! is_array($value)) { |
|
175
|
|
|
if ($key != $value) { |
|
176
|
|
|
$result[ $key ] = $value; |
|
177
|
|
|
$count++; |
|
178
|
|
|
} |
|
179
|
|
|
} else { |
|
180
|
|
|
$sub = array_filter_recursive($value, $value, $limit); |
|
181
|
|
|
if (count($sub) > 0) { |
|
182
|
|
|
if ($key != $value) { |
|
183
|
|
|
$result[ $key ] = $sub; |
|
184
|
|
|
$count += count($sub); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $result; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
// ------------------------------------------------------------------------ |
|
195
|
|
|
|
|
196
|
|
|
if ( ! function_exists('array_search_recursive')) { |
|
197
|
|
|
/** |
|
198
|
|
|
* array_search_recursive |
|
199
|
|
|
* |
|
200
|
|
|
* Searches the multidimensional array for a given value and returns the first corresponding key if successful. |
|
201
|
|
|
* |
|
202
|
|
|
* @param mixed $needle The searched value. |
|
203
|
|
|
* @param array $haystack The multidimensional array. |
|
204
|
|
|
* @param bool $strict |
|
205
|
|
|
* |
|
206
|
|
|
* @return bool|int|string |
|
207
|
|
|
*/ |
|
208
|
|
|
function array_search_recursive($needle, array $haystack, $strict = false) |
|
209
|
|
|
{ |
|
210
|
|
|
$result = ''; |
|
211
|
|
|
foreach ($haystack as $key => $value) { |
|
212
|
|
|
if ($strict === false) { |
|
213
|
|
|
$needle = strtolower($needle); |
|
214
|
|
|
|
|
215
|
|
|
if ( ! is_array($value)) { |
|
216
|
|
|
$value = strtolower($value); |
|
217
|
|
|
} else { |
|
218
|
|
|
$value = array_map('strtolower', $value); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
if ($needle === $value) { |
|
223
|
|
|
$result = $key; |
|
224
|
|
|
break; |
|
225
|
|
|
} elseif (is_array($value)) { |
|
226
|
|
|
if (array_search_recursive($needle, $value)) { |
|
227
|
|
|
$result = $key; |
|
228
|
|
|
break; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return ($result == '') ? false : $result; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
// ------------------------------------------------------------------------ |
|
238
|
|
|
|
|
239
|
|
|
if ( ! function_exists('array_unique_recursive')) { |
|
240
|
|
|
/** |
|
241
|
|
|
* array_unique_recursive |
|
242
|
|
|
* |
|
243
|
|
|
* Removes duplicate values from an multidimensional array. |
|
244
|
|
|
* |
|
245
|
|
|
* @param array $array Array Source |
|
246
|
|
|
* |
|
247
|
|
|
* @return array |
|
248
|
|
|
*/ |
|
249
|
|
|
function array_unique_recursive(array $array) |
|
250
|
|
|
{ |
|
251
|
|
|
$serialized = array_map('serialize', $array); |
|
252
|
|
|
$unique = array_unique($serialized); |
|
253
|
|
|
|
|
254
|
|
|
return array_intersect_key($array, $unique); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
// ------------------------------------------------------------------------ |
|
259
|
|
|
|
|
260
|
|
|
if ( ! function_exists('array_flatten')) { |
|
261
|
|
|
/** |
|
262
|
|
|
* array_flatten |
|
263
|
|
|
* |
|
264
|
|
|
* Merge an multidimensional array into regular array. |
|
265
|
|
|
* |
|
266
|
|
|
* @param array $array |
|
267
|
|
|
* |
|
268
|
|
|
* @return array |
|
269
|
|
|
*/ |
|
270
|
|
|
function array_flatten(array $array = []) |
|
271
|
|
|
{ |
|
272
|
|
|
$flat_array = []; |
|
273
|
|
|
|
|
274
|
|
|
foreach ($array as $key => $value) { |
|
275
|
|
|
if (is_array($value)) { |
|
276
|
|
|
$flat_array = array_merge($flat_array, array_flatten($value)); |
|
277
|
|
|
} else { |
|
278
|
|
|
$flat_array[ $key ] = $value; |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
return $flat_array; |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
// ------------------------------------------------------------------------ |
|
287
|
|
|
|
|
288
|
|
|
if ( ! function_exists('range_price')) { |
|
289
|
|
|
/** |
|
290
|
|
|
* range_price |
|
291
|
|
|
* |
|
292
|
|
|
* Create an array containing a range of prices. |
|
293
|
|
|
* |
|
294
|
|
|
* @param int $min The minimum price numeric value. |
|
295
|
|
|
* @param int $max The maximum price numeric value. |
|
296
|
|
|
* @param int $multiplier The multiplier used in the range, default is 0. |
|
297
|
|
|
* |
|
298
|
|
|
* @return array |
|
299
|
|
|
*/ |
|
300
|
|
|
function range_price($min, $max, $multiplier = 0) |
|
301
|
|
|
{ |
|
302
|
|
|
$multiplier = $multiplier * 20; |
|
303
|
|
|
$num_range = $max / $min; |
|
304
|
|
|
$num_step = $multiplier / $min / 100; |
|
305
|
|
|
|
|
306
|
|
|
$ranges = []; |
|
307
|
|
|
foreach (range(0, $num_range, $num_step) as $num_price) { |
|
308
|
|
|
if ($num_price == 0) { |
|
309
|
|
|
$ranges[] = $min; |
|
310
|
|
|
} else { |
|
311
|
|
|
$ranges[] = $num_price * $min / 2 * 10; |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
$prices = []; |
|
316
|
|
|
for ($i = 0; $i < count($ranges); $i++) { |
|
|
|
|
|
|
317
|
|
|
if ($ranges[ $i ] == $max) { |
|
318
|
|
|
break; |
|
319
|
|
|
} else { |
|
320
|
|
|
$prices[ $ranges[ $i ] ] = ($ranges[ $i + 1 ] == 0) ? $ranges[ $i ] * 2 : $ranges[ $i + 1 ]; |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
return $prices; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
// ------------------------------------------------------------------------ |
|
329
|
|
|
|
|
330
|
|
|
|
|
331
|
|
|
if ( ! function_exists('range_date')) { |
|
332
|
|
|
/** |
|
333
|
|
|
* range_date |
|
334
|
|
|
* |
|
335
|
|
|
* Creates an array containing a range of dates. |
|
336
|
|
|
* |
|
337
|
|
|
* @param string|int $start_date Start Date |
|
338
|
|
|
* @param int $days Num of days |
|
339
|
|
|
* |
|
340
|
|
|
* @return array |
|
341
|
|
|
*/ |
|
342
|
|
|
function range_date($start_date, $days = 1) |
|
343
|
|
|
{ |
|
344
|
|
|
$start_date = (is_string($start_date) ? strtotime($start_date) : $start_date); |
|
345
|
|
|
|
|
346
|
|
|
$date_range = []; |
|
347
|
|
|
for ($i = 0; $i < $days; $i++) { |
|
348
|
|
|
$date_range[ $i ] = $start_date + ($i * 24 * 60 * 60); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
return $date_range; |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
// ------------------------------------------------------------------------ |
|
355
|
|
|
|
|
356
|
|
|
if ( ! function_exists('range_year')) { |
|
357
|
|
|
/** |
|
358
|
|
|
* range_year |
|
359
|
|
|
* |
|
360
|
|
|
* Create an array containing a range of years. |
|
361
|
|
|
* |
|
362
|
|
|
* @param int $min The minimum numeric year value. |
|
363
|
|
|
* @param null $max The maximum numeric year value. |
|
|
|
|
|
|
364
|
|
|
* @param int $step The increment used in the range, default is 1. |
|
365
|
|
|
* |
|
366
|
|
|
* @return array |
|
367
|
|
|
*/ |
|
368
|
|
|
function range_year($min = 1995, $max = null, $step = 1) |
|
369
|
|
|
{ |
|
370
|
|
|
$max = empty($max) ? date('Y') : $max; |
|
371
|
|
|
|
|
372
|
|
|
$years = []; |
|
373
|
|
|
|
|
374
|
|
|
foreach (range($min, $max, $step) as $year) { |
|
375
|
|
|
$years[ $year ] = $year; |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
return $years; |
|
379
|
|
|
} |
|
380
|
|
|
} |