forked from M1ke/easy-site-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.php
609 lines (508 loc) · 11.2 KB
/
array.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
<?php
// generates the php code to create a selected array
function array_code($arr){
if (is_array($arr)){
return var_export($arr);
}
return '';
}
function array_assoc($arr){
$assoc = [];
foreach ($arr as $key => $val){
$assoc[$val] = $key;
}
return $assoc;
}
function array_overwrite($base, $fill){
if (empty($fill)){
return $base;
}
$arr = [];
foreach ($base as $n => $key){
if (isset($fill[$key])){
$arr[$key] = $fill[$key];
}
else {
$arr[$n] = $key;
}
}
return $arr;
}
function array_extract(array $arr, $field){
$return = [];
foreach ($arr as $key => $val){
$return[$key] = $val[$field];
}
return $return;
}
// places items in a flat array into a multidimensional array based on keys
function array_id(&$arr, $key, $key2 = null, $key3 = null){
if (is_array($arr)){
$new = [];
foreach ($arr as $item){
if ($key3){
$new[$item[$key3]][$item[$key2]][$item[$key]] = $item;
}
elseif ($key2) {
$new[$item[$key2]][$item[$key]] = $item;
}
else {
$new[$item[$key]] = $item;
}
}
$arr = $new;
}
}
function array_data_sort(Array $arr, $sort_field, $desc = false){
$first = reset($arr);
if (!isset($first[$sort_field])){
return $arr;
}
foreach ($arr as $key => $item){
$sort[$key] = $item[$sort_field];
}
array_multisort($sort, $desc ? SORT_DESC : SORT_ASC, $arr);
return $arr;
}
function array_union(Array $a, Array $b){
return array_unique(array_merge($a, $b));
}
function array_unserialize($string){
$arr = unserialize($string);
if ($arr===false){
$arr = [];
}
return $arr;
}
// shifts all array keys by a specified value (default reduces by 1)
function array_key_shift(&$arr, $num = -1){
$temp = [];
foreach ($arr as $key => $val){
$key += $num;
$temp[$key] = $val;
}
$arr = $temp;
unset($temp);
}
function array_keys_2d($arr){
$first_row = reset($arr);
return array_keys($first_row);
}
// takes specific keys out of an array and returns the result
function array_pull(array $arr, $pull, &$new = []){
if (!is_array($pull)){
$pull = explode(',', $pull);
}
foreach ($pull as $key => $field){
$field = trim($field);
$key = is_numeric($key) ? $field : $key;
$new[$field] = $arr[$key];
}
return $new;
}
// reutrns the first item of an array. Not certain where we needed this, reset($arr) does the same!
function arraystr($array){
foreach ($array as $item){
return (string)$item;
}
return '';
}
// flattens multiple keys of an array into a single dimension
function array_oned(&$arr, $assoc, $pre){
if (is_array($arr[$assoc])){
foreach ($arr[$assoc] as $key => $val){
$arr[$pre.'-'.$key] = $val;
}
}
unset($arr[$assoc]);
}
function array_random(Array $arr){
$keys = array_keys($arr);
$key = $keys[rand(0, count($keys)-1)];
return $arr[$key];
}
function array_remove($needle, Array $haystack){
$key = array_search($needle, $haystack);
if (!empty($key)){
unset($haystack[$key]);
}
return $haystack;
}
function array_remove_empty(Array $arr){
foreach ($arr as $key => $val){
if (empty($val)){
unset($arr[$key]);
}
}
return $arr;
}
// inserts an item into an array at a specified key without creating a new array
function array_slip($arr, $assign, $overwrite_or_val = true, $overwrite = true){
if (!is_array($assign)){
$assign = [$assign => $overwrite_or_val];
}
else {
$overwrite = $overwrite_or_val;
}
foreach ($assign as $key => $val){
if ($overwrite or !isset($arr[$key])){
$arr[$key] = $val;
}
}
return $arr;
}
function array_slip_2d($arr, $slip, $val = null){
if (!is_array($slip)){
$slip = [$slip => $val];
}
foreach ($arr as &$sub_arr){
foreach ($slip as $sub_key => $sub_val){
$sub_arr = array_slip($sub_arr, $sub_key, $sub_val);
}
}
return $arr;
}
// opposite of array_slip, removes one item from an array without creating a new array
function array_snip($arr, $key){
unset($arr[$key]);
return $arr;
}
function array_invert($item, $item_key, &$twod, $include = [], $ext = ''){
foreach ($item as $key => $value){
if (empty($include) || in_array($key, $include)){
$twod[$key.$ext][$item_key] = $value;
}
}
return $twod;
}
function array_stitch(Array $arr, Array $order, $glue = '', $missing = false){
$new_arr = [];
foreach ($order as $key){
$new_arr[] = $arr[$key];
if ($missing){
unset($arr[$key]);
}
}
if ($missing){
foreach ($arr as $val){
$new_arr[] = $val;
}
}
return implode($glue, $new_arr);
}
function array_stitch_empty(Array $arr, Array $order, $glue = ''){
foreach ($order as $n => $key){
if (empty($arr[$key])){
unset($order[$n]);
}
}
return array_stitch($arr, $order, $glue);
}
function array_strip_end(array $arr){
$assoc = is_assoc($arr);
$n = count($arr)-1;
while (empty($arr[$n]) && $n>=0){
unset($arr[$n]);
$n--;
}
return $assoc ? $arr : array_values($arr);
}
function array_strip_start(array $arr){
$assoc = is_assoc($arr);
$count = count($arr)-1;
$n = 0;
while (empty($arr[$n]) && $n<=$count){
unset($arr[$n]);
$n++;
}
return $assoc ? $arr : array_values($arr);
}
function array_strip(array $arr){
$arr = array_strip_start($arr);
$arr = array_strip_end($arr);
return $arr;
}
// inserts an item between specific keys in an array. useful when relying on the iteration order of an array
function array_insert_assoc($arr, $offset, $insert, $before = false){
$keys = array_keys($arr);
$offset = array_search($offset, $keys, true);
if ($before and $offset>0){
$offset--;
}
$temp = [];
$n = 0;
$adj = 0;
foreach ($arr as $key => $val){
$temp[is_numeric($key) ? ($key+$adj) : $key] = $val;
if ($n==$offset){
if (is_array($insert)){
foreach ($insert as $ins_key => $ins_val){
$temp[$ins_key] = $ins_val;
}
}
else {
$temp[] = $insert;
$adj++;
}
}
$n++;
}
return $temp;
}
function array_search_2d($needle, $haystack, $key, $last = false){
$found = false;
foreach ($haystack as $index => $item){
if ($item[$key]==$needle){
$found = $index;
if (!$last){
break;
}
}
}
return $found;
}
function array_switch($arr, $key1, $key2){
$temp = [];
foreach ($arr as $key => $val){
if ($key===$key1){
$temp[$key2] = $arr[$key2];
}
elseif ($key===$key2) {
$temp[$key1] = $arr[$key1];
}
else {
$temp[$key] = $val;
}
}
return $temp;
}
// builds a compressed array up into a multidimensional array
function array_twod(&$arr, $assoc, $pre){
$keys = array_keys($arr);
foreach ($keys as $key){
if (strpos($key, $pre)!==false){
if (is_array($arr[$key])){
foreach ($arr[$key] as $n => $val){
$arr[$assoc][$n][str_replace($pre.'-', '', $key)] = $val;
}
}
else {
$arr[$assoc][str_replace($pre.'-', '', $key)] = $arr[$key];
}
unset($arr[$key]);
}
}
}
// unsets specific keys in an array
function array_unset($arr, $keys){
foreach ($keys as $key){
unset($arr[$key]);
}
return $arr;
}
// unsets items in an array where the value is false or empty
function array_unset_false($arr, $strict = true){
foreach ($arr as $key => $val){
if ($strict and $val===false){
unset($arr[$key]);
}
elseif ($val=='') {
unset($arr[$key]);
}
}
return $arr;
}
function average($arr){
$avg = array_sum($arr) / count($arr);
return $avg;
}
function flatten_array($array){
$new = [];
if (is_array($array)){
foreach ($array as $item){
if (is_array($item)){
$item = arraystr($item);
}
$new[] = $item;
}
}
return $new;
}
function implode_assoc($sep, $arr){
$string = '';
foreach ($arr as $key => $val){
$string .= $val;
if (!is_numeric($key) and $val!=end($arr)){
$string .= $sep;
}
}
return $string;
}
function implode_dual($sep, $arr1, $arr2){
$string = '';
foreach ($arr1 as $key => $val){
$string .= $val.$sep[1].$arr2[$key];
if ($val!=end($arr1)){
$string .= $sep[0];
}
}
return $string;
}
function implode_key($sep, $arr){
foreach ($arr as $key => &$val){
$val = $key;
}
$arr = implode($sep, $arr);
return $arr;
}
function implode_wrap($pre, $suf, $arr){
$html = '';
if (is_array($arr)){
foreach ($arr as $val){
$html .= $pre.$val.$suf;
}
}
return $html;
}
function is_array_full($arr){
if (!is_array($arr)){
return false;
}
if (empty($arr)){
return false;
}
foreach ($arr as $item){
if (!empty($item)){
return true;
}
}
return false;
}
function is_array_true($arr){
if (!is_array($arr)){
return false;
}
$return = false;
foreach ($arr as $item){
if ($item!==false){
$return = true;
}
}
return $return;
}
function is_assoc(&$arr){
if (is_array($arr)){
return !(array_values($arr)===$arr);
}
return false;
}
function largest_array(){
$arrs = func_get_args();
$large = 0;
$largest = 0;
foreach ($arrs as $key => $arr){
$count = count($arr);
if ($count>$large){
$large = $count;
$largest = $key;
}
}
return $arrs[$largest];
}
function largest_array_count(){
$arrs = func_get_args();
$large = 0;
foreach ($arrs as $arr){
$count = count($arr);
if ($count>$large){
$large = $count;
}
}
return $large;
}
function make_array($count){
$arr = [];
for ($n = 0; $n<$count; $n++){
$arr[$n] = 1;
}
return $arr;
}
function number_list($to = 10, $start = 1){
$arr = [];
for ($n = $start; $n<=$to; $n++){
$arr[] = $n;
}
return $arr;
}
function super_implode($sep, $arr, $assoc){
$temp = [];
foreach ($arr as $val){
$temp[] = $val[$assoc];
}
$temp = implode($sep, $temp);
return $temp;
}
// returns true if any of the keys exist in the array
// by default uses OR condition
function array_keys_exist($keys, $arr, $and = false){
$result = $and;
foreach ($keys as $key){
$result = $and ? $result && array_key_exists($key, $arr) : $result || array_key_exists($key, $arr);
}
return $result;
}
function as_array($item){
if (!is_array($item)){
$item = [$item];
}
return $item;
}
/**
* Verifies, recursively, existence of keys and if key values are an array
* Will return an empty array is verified
*
* @param array $format
* @param array $check
* @param string $depth
* @param array $errors
* @return array
*/
function array_keys_verify(array $format, array $check, $depth = '', $errors = []){
foreach ($format as $key => $sub_format){
$current_depth = $depth.'.'.$key;
// First see if they key is even set
if (!isset($check[$key])){
$errors[$current_depth] = "The key '$key' must be set, even if it is blank";
continue;
}
if (is_null($sub_format)){
continue;
}
// If the sub-format isn't an array we're done with this key
if (!is_array($sub_format)){
$type = gettype($check[$key]);
if ($sub_format==='non-zero'){
// non-zero isn't a regular type, but requiring non-zero values
// can be as relevant to operational logic, preventing div by zero etc
if ((int)$check[$key]===0){
$errors[$current_depth] = "The key '$key' must be a non-zero integer";
}
}
elseif ($type!==$sub_format) {
$errors[$current_depth] = "The key '$key' must be of type '$sub_format', not '$type''";
}
continue;
}
// If sub-format is an array and the value we're checking isn't, that's a problem
if (!is_array($check[$key])){
$errors[$current_depth] = "The key '$key' must be an array, even if it is empty";
continue;
}
// If our sub-format is an array with values we then dive into that
if (!empty($sub_format)){
$errors = array_keys_verify($sub_format, $check[$key], $current_depth, $errors);
continue;
}
}
return $errors;
}