-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.php
More file actions
128 lines (100 loc) · 2.86 KB
/
Copy patharray.php
File metadata and controls
128 lines (100 loc) · 2.86 KB
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
<?php
// Array utilities.
function assoc($term) {
if(is_object($term)) $term = get_object_vars($term);
if(is_array($term)) return array_map(__FUNCTION__, $term);
return $term;
}
function zip($separator, $array) {
$keys = array_keys($array);
$values = array_values($array);
return array_map(function($key, $value) use ($separator) {
return $key . $separator . $value;
}, $keys, $values);
}
function flatten($array) {
return array_reduce($array, function($acc, $item) {
return array_merge($acc, is_array($item) ? flatten($item) : [$item]);
}, []);
}
function count_by($array, $key) {
$keys = array_column($array, $key);
$array = array_reduce($keys, function($acc, $key) {
if(isset($acc[$key])) $acc[$key]++;
else $acc[$key] = 1;
return $acc;
}, []);
arsort($array); // Ew, in-place mutation.
return $array;
}
function group_by($items, $prefix) {
$grouped = [];
foreach($items as $item) {
$id = $item[$prefix . "_id"];
if(!isset($grouped[$id])) {
$grouped[$id] = array_merge(
unprefix_keys($item, $prefix),
['items' => []]
);
}
$grouped[$id]['items'][] = $item;
}
return $grouped;
}
function collect_by($items, $prefix, $as) {
$grouped = [];
$prefix_str = $prefix . '_';
foreach($items as $item) {
$id = $item['id'];
if(!isset($grouped[$id])) {
$base = array_filter($item, fn($k) => !str_starts_with($k, $prefix_str), ARRAY_FILTER_USE_KEY);
$grouped[$id] = array_merge($base, [$as => []]);
}
$nested = unprefix_keys($item, $prefix);
if(array_filter($nested, fn($v) => $v !== null)) {
$grouped[$id][$as][] = $nested;
}
}
return array_values($grouped);
}
function find_by($haystack, $key, $value) {
foreach($haystack as $item)
if(@$item[$key] === $value) return $item;
return null;
}
function take($array, $amount) {
return array_slice($array, 0, $amount);
}
function map($array, $key_k, $value_k) {
return array_reduce($array, function($acc, $item) {
$acc[$item[$key_k]] = $item[$value_k];
return $acc;
}, []);
}
function prefix_keys($array, $prefix) {
return array_combine(
array_map(fn($k) => "$prefix$k", array_keys($array)),
array_values($array)
);
}
function unprefix_keys($array, $prefix) {
$filtered = [];
$prefix = $prefix."_";
$len = strlen($prefix);
foreach($array as $key => $value) {
if(strpos($key, $prefix) === 0) {
$filtered[substr($key, $len)] = $value;
}
}
return $filtered;
}
function drop_empty($array) {
return array_filter($array, fn($value) => !in_array($value, ["", null, false]));
}
function deep_contains($haystack, $needle) {
return $needle and count(array_filter($haystack, fn($candidate) => strpos($needle, $candidate) != false)) > 0;
}
function allset($array, $keys) {
foreach($keys as $key) if(!isset($array[$key])) return false;
return true;
}