|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace cse\helpers; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Arrays |
|
9
|
|
|
* |
|
10
|
|
|
* @package cse\helpers |
|
11
|
|
|
*/ |
|
12
|
|
|
class Arrays |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Get array value by key |
|
16
|
|
|
* |
|
17
|
|
|
* @param array $data |
|
18
|
|
|
* @param $key |
|
19
|
|
|
* @param $default |
|
20
|
|
|
* |
|
21
|
|
|
* @return mixed|null |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function get(array $data, $key, $default = null) |
|
24
|
|
|
{ |
|
25
|
|
|
if (!array_key_exists($key, $data)) return $default; |
|
26
|
|
|
|
|
27
|
|
|
return $data[$key]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Pull array key |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $data |
|
34
|
|
|
* @param $key |
|
35
|
|
|
* @param $default |
|
36
|
|
|
* |
|
37
|
|
|
* @return mixed|null |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function pullKey(array &$data, $key, $default = null) |
|
40
|
|
|
{ |
|
41
|
|
|
$result = self::get($data, $key, $default); |
|
42
|
|
|
unset($data[$key]); |
|
43
|
|
|
|
|
44
|
|
|
return $result; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Object to array |
|
49
|
|
|
* |
|
50
|
|
|
* @param $data |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function objectToArray($data): array |
|
55
|
|
|
{ |
|
56
|
|
|
return json_decode(json_encode($data), true); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Array to html |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $data |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function toTags(array $data): string |
|
67
|
|
|
{ |
|
68
|
|
|
$result = []; |
|
69
|
|
|
|
|
70
|
|
|
foreach ($data as $key => $item) { |
|
71
|
|
|
if (is_int($key)) { |
|
72
|
|
|
$tag = $item; |
|
73
|
|
|
} else { |
|
74
|
|
|
$tag = $key; |
|
75
|
|
|
|
|
76
|
|
|
if (is_array($item)) { |
|
77
|
|
|
$options = ''; |
|
78
|
|
|
$content = ''; |
|
79
|
|
|
|
|
80
|
|
|
foreach ($item as $key2 => $item2) { |
|
81
|
|
|
if (is_int($key2) && is_array($item2)) { |
|
82
|
|
|
foreach ($item2 as $name => $value) { |
|
83
|
|
|
$options .= ' ' . $name .'="' . $value . '"'; |
|
84
|
|
|
} |
|
85
|
|
|
} elseif (is_int($key2)) { |
|
86
|
|
|
$content = $item2; |
|
87
|
|
|
} else { |
|
88
|
|
|
$options .= ' ' . $key2 .'="' . $item2 . '"'; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} else { |
|
93
|
|
|
$content = $item; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$result[] = '<' . $tag . (empty($options) ? '' : $options) . (empty($content) ? ' />' : '>' . $content . '</' . $tag . '>'); |
|
98
|
|
|
unset($options, $content, $tag); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return implode(PHP_EOL, $result); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Building Maps |
|
106
|
|
|
* |
|
107
|
|
|
* @param $data |
|
108
|
|
|
* @param $keyGroup |
|
109
|
|
|
* @param $keyValue |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function map(array $data, $keyGroup, $keyValue = null): array |
|
114
|
|
|
{ |
|
115
|
|
|
$result = []; |
|
116
|
|
|
|
|
117
|
|
|
foreach ($data as $item) { |
|
118
|
|
|
if (array_key_exists($keyGroup, $item)) { |
|
119
|
|
|
$result[$item[$keyGroup]] = is_null($keyValue) ? $item : (array_key_exists($keyValue, $item) ? $item[$keyValue] : null); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
unset($item); |
|
124
|
|
|
|
|
125
|
|
|
return $result; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Group array |
|
130
|
|
|
* |
|
131
|
|
|
* @param array $data |
|
132
|
|
|
* @param $keyGroup |
|
133
|
|
|
* @param $keyValue |
|
134
|
|
|
* |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function group(array $data, $keyGroup, $keyValue = null): array |
|
138
|
|
|
{ |
|
139
|
|
|
$result = []; |
|
140
|
|
|
|
|
141
|
|
|
foreach ($data as $item) { |
|
142
|
|
|
if (array_key_exists($keyGroup, $item)) { |
|
143
|
|
|
$key = $item[$keyGroup]; |
|
144
|
|
|
|
|
145
|
|
|
if (empty($result[$key])) $result[$key] = []; |
|
146
|
|
|
|
|
147
|
|
|
$result[$key][] = is_null($keyValue) ? $item : (array_key_exists($keyValue, $item) ? $item[$keyValue] : null); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
unset($item, $key); |
|
152
|
|
|
|
|
153
|
|
|
return $result; |
|
154
|
|
|
} |
|
155
|
|
|
/** |
|
156
|
|
|
* Index group array |
|
157
|
|
|
* |
|
158
|
|
|
* @param array $data |
|
159
|
|
|
* @param $keyGroup |
|
160
|
|
|
* @param $keyValue |
|
161
|
|
|
* |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
|
|
public static function index(array $data, $keyGroup, $keyValue = null): array |
|
165
|
|
|
{ |
|
166
|
|
|
$result = []; |
|
167
|
|
|
|
|
168
|
|
|
foreach ($data as $item) { |
|
169
|
|
|
if (array_key_exists($keyGroup, $item)) { |
|
170
|
|
|
$result[$item[$keyGroup]][] = is_null($keyValue) ? $item : (array_key_exists($keyValue, $item) ? $item[$keyValue] : null); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
unset($item); |
|
175
|
|
|
|
|
176
|
|
|
return $result; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Append not empty array data |
|
181
|
|
|
* |
|
182
|
|
|
* @param array $first |
|
183
|
|
|
* @param array $second |
|
184
|
|
|
* |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
187
|
|
|
public static function appendNotEmptyData(array $first, array $second): array |
|
188
|
|
|
{ |
|
189
|
|
|
$result = null; |
|
190
|
|
|
|
|
191
|
|
|
switch (true) { |
|
192
|
|
|
case empty($first) && empty($second): |
|
193
|
|
|
$result = []; |
|
194
|
|
|
break; |
|
195
|
|
|
case empty($second): |
|
196
|
|
|
$result = $first; |
|
197
|
|
|
break; |
|
198
|
|
|
case empty($first): |
|
199
|
|
|
$result = self::removeEmpty($second); |
|
200
|
|
|
break; |
|
201
|
|
|
default: |
|
202
|
|
|
foreach ($second as $key => $value) { |
|
203
|
|
|
if (!empty($value) && !array_key_exists($key, $first)) $first[$key] = $value; |
|
204
|
|
|
} |
|
205
|
|
|
$result = $first; |
|
206
|
|
|
break; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $result; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Replace empty array data to not empty array data |
|
214
|
|
|
* |
|
215
|
|
|
* @param array $first |
|
216
|
|
|
* @param array $second |
|
217
|
|
|
* |
|
218
|
|
|
* @return array |
|
219
|
|
|
*/ |
|
220
|
|
|
public static function replaceEmptyNotEmptyData(array $first, array $second): array |
|
221
|
|
|
{ |
|
222
|
|
|
if (empty($first)) { |
|
223
|
|
|
return []; |
|
224
|
|
|
} elseif (empty($second)) { |
|
225
|
|
|
return $first; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
foreach ($first as $key => &$value) { |
|
229
|
|
|
if (empty($value) && array_key_exists($key, $second)) $value = $second[$key]; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
unset($value, $key, $second); |
|
233
|
|
|
|
|
234
|
|
|
return $first; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Replace not empty array data |
|
239
|
|
|
* |
|
240
|
|
|
* @param array $first |
|
241
|
|
|
* @param array $second |
|
242
|
|
|
* |
|
243
|
|
|
* @return array |
|
244
|
|
|
*/ |
|
245
|
|
|
public static function replaceNotEmptyData(array $first, array $second): array |
|
246
|
|
|
{ |
|
247
|
|
|
if (empty($first)) { |
|
248
|
|
|
return []; |
|
249
|
|
|
} elseif (empty($second)) { |
|
250
|
|
|
return $first; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
foreach ($first as $key => &$value) { |
|
254
|
|
|
if (!empty($second[$key])) $value = $second[$key]; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
unset($value, $key, $second); |
|
258
|
|
|
|
|
259
|
|
|
return $first; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Merge not empty array data |
|
264
|
|
|
* |
|
265
|
|
|
* @param array $first |
|
266
|
|
|
* @param array $second |
|
267
|
|
|
* |
|
268
|
|
|
* @return array |
|
269
|
|
|
*/ |
|
270
|
|
|
public static function mergeNotEmptyData(array $first, array $second): array |
|
271
|
|
|
{ |
|
272
|
|
|
if (empty($first) && empty($second)) { |
|
273
|
|
|
return []; |
|
274
|
|
|
} elseif (empty($second)) { |
|
275
|
|
|
return $first; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
foreach ($second as $key => $value) { |
|
279
|
|
|
if (!empty($value)) $first[$key] = $value; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
unset($value, $key); |
|
283
|
|
|
|
|
284
|
|
|
return $first; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Convert empty to null |
|
289
|
|
|
* |
|
290
|
|
|
* @param array $data |
|
291
|
|
|
* @param bool $recursive |
|
292
|
|
|
* |
|
293
|
|
|
* @return array |
|
294
|
|
|
*/ |
|
295
|
|
|
public static function emptyToNull(array $data, bool $recursive = false): array |
|
296
|
|
|
{ |
|
297
|
|
|
foreach ($data as $key => &$value) { |
|
298
|
|
|
if (empty($value)) { |
|
299
|
|
|
$value = null; |
|
300
|
|
|
} elseif ($recursive && is_array($value)) { |
|
301
|
|
|
$value = self::emptyToNull($value, $recursive); |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
unset($value, $key); |
|
306
|
|
|
|
|
307
|
|
|
return $data; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Remove empty data to array |
|
312
|
|
|
* |
|
313
|
|
|
* @param array $data |
|
314
|
|
|
* @param bool $recursive |
|
315
|
|
|
* |
|
316
|
|
|
* @return array |
|
317
|
|
|
*/ |
|
318
|
|
|
public static function removeEmpty(array $data, bool $recursive = false): array |
|
319
|
|
|
{ |
|
320
|
|
|
foreach ($data as $key => &$value) { |
|
321
|
|
|
if (empty($value)) { |
|
322
|
|
|
unset($data[$key]); |
|
323
|
|
|
} elseif ($recursive && is_array($value)) { |
|
324
|
|
|
$value = self::removeEmpty($value, $recursive); |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
unset($value, $key); |
|
329
|
|
|
|
|
330
|
|
|
return $data; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Remove null data to array |
|
335
|
|
|
* |
|
336
|
|
|
* @param array $data |
|
337
|
|
|
* @param bool $recursive |
|
338
|
|
|
* |
|
339
|
|
|
* @return array |
|
340
|
|
|
*/ |
|
341
|
|
|
public static function removeNull(array $data, bool $recursive = false): array |
|
342
|
|
|
{ |
|
343
|
|
|
foreach ($data as $key => &$value) { |
|
344
|
|
|
if (is_null($value)) { |
|
345
|
|
|
unset($data[$key]); |
|
346
|
|
|
} elseif ($recursive && is_array($value)) { |
|
347
|
|
|
$value = self::removeNull($value, $recursive); |
|
348
|
|
|
} |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
unset($value, $key); |
|
352
|
|
|
|
|
353
|
|
|
return $data; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* Trim value to array |
|
358
|
|
|
* |
|
359
|
|
|
* @param array $data |
|
360
|
|
|
* @param bool $recursive |
|
361
|
|
|
* |
|
362
|
|
|
* @return array |
|
363
|
|
|
*/ |
|
364
|
|
|
public static function trim(array $data, bool $recursive = true): array |
|
365
|
|
|
{ |
|
366
|
|
|
foreach ($data as $key => &$value) { |
|
367
|
|
|
if (is_string($value)) { |
|
368
|
|
|
$value = trim($value); |
|
369
|
|
|
} elseif ($recursive && is_array($value)) { |
|
370
|
|
|
$value = self::trim($value); |
|
371
|
|
|
} |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
unset($value, $key); |
|
375
|
|
|
|
|
376
|
|
|
return $data; |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
|