|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ArrayHelper; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Array helper |
|
7
|
|
|
* * improve managing array. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Dimas Lanjaka <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class helper |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Unset multiple keys in array. |
|
15
|
|
|
* |
|
16
|
|
|
* @todo Remove all defined keys in array |
|
17
|
|
|
* |
|
18
|
|
|
* @param array $source |
|
19
|
|
|
* @param array $keys |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function unset(array $source, array $keys) |
|
22
|
|
|
{ |
|
23
|
|
|
foreach ($keys as $unset) { |
|
24
|
|
|
if (isset($source[$unset])) { |
|
25
|
|
|
unset($source[$unset]); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return $source; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function recursiveFind(array $haystack, $needle) |
|
33
|
|
|
{ |
|
34
|
|
|
$iterator = new \RecursiveArrayIterator($haystack); |
|
35
|
|
|
$recursive = new \RecursiveIteratorIterator( |
|
36
|
|
|
$iterator, |
|
37
|
|
|
\RecursiveIteratorIterator::SELF_FIRST |
|
38
|
|
|
); |
|
39
|
|
|
foreach ($recursive as $key => $value) { |
|
40
|
|
|
if ($key === $needle) { |
|
41
|
|
|
yield $value; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public static function array_search(array $haystack, $needle) |
|
47
|
|
|
{ |
|
48
|
|
|
if (!self::isAssoc($haystack)) { |
|
49
|
|
|
throw new \MVC\Exception('Array search only for associative array', 1); |
|
50
|
|
|
} |
|
51
|
|
|
$result = []; |
|
52
|
|
|
$search = self::recursiveFind($haystack, $needle); |
|
53
|
|
|
//var_dump($search); |
|
54
|
|
|
foreach ($search as $value) { |
|
55
|
|
|
// Use `$value` here |
|
56
|
|
|
$result[] = $value; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $result; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
static function recursive_array_search($needle, $haystack) |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
foreach ($haystack as $key => $value) { |
|
65
|
|
|
$current_key = $key; |
|
66
|
|
|
if ($needle === $value or (is_array($value) && self::recursive_array_search($needle, $value) !== false)) { |
|
67
|
|
|
return $current_key; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Check array has All properties. |
|
75
|
|
|
* |
|
76
|
|
|
* @todo check all keys in array |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $array |
|
79
|
|
|
* @param array $key |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function hasAll(array $array, array $key) |
|
84
|
|
|
{ |
|
85
|
|
|
if (empty($key)) { |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
$existAll = true; |
|
89
|
|
|
foreach ($key as $val) { |
|
90
|
|
|
if (!array_key_exists($val, $array)) { |
|
91
|
|
|
$existAll = false; |
|
92
|
|
|
//var_dump($val, $array); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $existAll; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public static function random_array_n(int $n, array $arr) |
|
100
|
|
|
{ |
|
101
|
|
|
$result = []; |
|
102
|
|
|
if (empty($arr)) { |
|
103
|
|
|
return $result; |
|
104
|
|
|
} |
|
105
|
|
|
shuffle($arr); |
|
106
|
|
|
for ($i = 0; $i < $n; ++$i) { |
|
107
|
|
|
$result[] = $arr[array_rand($arr)]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $result; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public static function replace_value_by_key(array $before, array $after) |
|
114
|
|
|
{ |
|
115
|
|
|
foreach ($before as $key => $value) { |
|
116
|
|
|
if (isset($after[$key])) { |
|
117
|
|
|
$before[$key] = $after[$key]; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $before; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public static function get_rand_arr(array $items, int $length = 1) |
|
125
|
|
|
{ |
|
126
|
|
|
$result = []; |
|
127
|
|
|
for ($i = 0; $i < $length; ++$i) { |
|
128
|
|
|
$result[] = $items[array_rand($items)]; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $result; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public static function fix_undefined_properties(array $arr, array $name) |
|
135
|
|
|
{ |
|
136
|
|
|
foreach ($name as $prop => $callback) { |
|
137
|
|
|
if (!isset($arr[$prop])) { |
|
138
|
|
|
$arr[$prop] = $callback; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $arr; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public static function get(array $source, array $keys = []) |
|
146
|
|
|
{ |
|
147
|
|
|
$source = (array) $source; |
|
148
|
|
|
$result = []; |
|
149
|
|
|
foreach ($keys as $unset) { |
|
150
|
|
|
$replacement_value = false; |
|
151
|
|
|
$replacement_key = false; |
|
152
|
|
|
if (is_array($unset)) { |
|
153
|
|
|
$keys = array_keys($unset); |
|
154
|
|
|
$replacement_key = $keys[0]; |
|
155
|
|
|
$values = array_values($unset); |
|
156
|
|
|
$replacement_value = $values[0]; |
|
157
|
|
|
if (isset($source[$replacement_key])) { |
|
158
|
|
|
$result[$replacement_value] = $source[$replacement_key]; |
|
159
|
|
|
} |
|
160
|
|
|
continue; |
|
161
|
|
|
} |
|
162
|
|
|
if (isset($source[$unset])) { |
|
163
|
|
|
$result[$unset] = $source[$unset]; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $result; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public static function get_value_of_key(string $key, $source) |
|
171
|
|
|
{ |
|
172
|
|
|
if (array_key_exists($key, $source)) { |
|
173
|
|
|
return $source[$key]; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Is Associative array. |
|
179
|
|
|
* |
|
180
|
|
|
* @param array $source |
|
181
|
|
|
* |
|
182
|
|
|
* @return bool |
|
183
|
|
|
*/ |
|
184
|
|
|
public static function isAssoc(array $source) |
|
185
|
|
|
{ |
|
186
|
|
|
if ([] === $source) { |
|
187
|
|
|
return false; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return array_keys($source) !== range(0, count($source) - 1); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Is Sequental array. |
|
195
|
|
|
* |
|
196
|
|
|
* @param array $source |
|
197
|
|
|
* |
|
198
|
|
|
* @return bool |
|
199
|
|
|
*/ |
|
200
|
|
|
public static function isSequent(array $source) |
|
201
|
|
|
{ |
|
202
|
|
|
return !self::isAssoc($source); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* IS array or object |
|
207
|
|
|
* |
|
208
|
|
|
* @param array|object $objarr |
|
209
|
|
|
* @return boolean |
|
210
|
|
|
*/ |
|
211
|
|
|
public static function is_iterable($objarr) |
|
212
|
|
|
{ |
|
213
|
|
|
return is_array($objarr) || is_object($objarr); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.