1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace __\Traits; |
4
|
|
|
|
5
|
|
|
trait Arrays |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Append item to array |
9
|
|
|
* |
10
|
|
|
** __::append([1, 2, 3], 4); |
11
|
|
|
** // → [1, 2, 3, 4] |
12
|
|
|
* |
13
|
|
|
* @param array $array original array |
14
|
|
|
* @param null $value new item or valie to append |
|
|
|
|
15
|
|
|
* |
16
|
|
|
* @return array |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
1 |
|
public static function append(array $array = [], $value = null) |
20
|
|
|
{ |
21
|
1 |
|
$array[] = $value; |
22
|
|
|
|
23
|
1 |
|
return $array; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Creates an array with all falsey values removed. The values |
28
|
|
|
* false, null, 0, "", undefined, and NaN are all falsey. |
29
|
|
|
* |
30
|
|
|
** __::compact([0, 1, false, 2, '', 3]); |
31
|
|
|
** // → [1, 2, 3] |
32
|
|
|
* |
33
|
|
|
* @param array $array array to compact |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
* |
37
|
|
|
*/ |
38
|
3 |
|
public static function compact(array $array = []) |
39
|
|
|
{ |
40
|
3 |
|
$result = []; |
41
|
|
|
|
42
|
3 |
|
foreach ($array as $value) { |
43
|
3 |
|
if ($value) { |
44
|
3 |
|
$result[] = $value; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
return $result; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* base flatten |
54
|
|
|
* |
55
|
|
|
* @param array $array |
56
|
|
|
* @param bool $shallow |
57
|
|
|
* @param bool $strict |
58
|
|
|
* @param int $startIndex |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
* |
62
|
|
|
*/ |
63
|
1 |
|
public static function baseFlatten(array $array, $shallow = false, $strict = true, $startIndex = 0) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
|
66
|
1 |
|
$output = []; |
67
|
1 |
|
$idx = 0; |
68
|
|
|
|
69
|
1 |
|
foreach ($array as $index => $value) { |
70
|
1 |
|
if (is_array($value)) { |
71
|
1 |
|
if (!$shallow) { |
72
|
1 |
|
$value = static::baseFlatten($value, $shallow, $strict); |
73
|
|
|
} |
74
|
1 |
|
$j = 0; |
75
|
1 |
|
$len = count($value); |
76
|
1 |
|
while ($j < $len) { |
77
|
1 |
|
$output[$idx++] = $value[$j++]; |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
1 |
|
if (!$strict) { |
81
|
1 |
|
$output[$idx++] = $value; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
return $output; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Flattens a multidimensional array. If you pass shallow, the array will only be flattened a single level. |
91
|
|
|
* |
92
|
|
|
* __::flatten([1, 2, [3, [4]]], [flatten]); |
93
|
|
|
* >> [1, 2, 3, 4] |
94
|
|
|
* |
95
|
|
|
* @param $array |
96
|
|
|
* @param bool $shallow |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
* |
100
|
|
|
*/ |
101
|
1 |
|
public static function flatten($array, $shallow = false) |
102
|
|
|
{ |
103
|
1 |
|
return static::baseFlatten($array, $shallow, false); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Patches array by xpath. |
108
|
|
|
* |
109
|
|
|
** __::patch(['addr' => ['country' => 'US', 'zip' => 12345]], ['/addr/country' => 'CA', '/addr/zip' => 54321]); |
110
|
|
|
** // → ['addr' => ['country' => 'CA', 'zip' => 54321]] |
111
|
|
|
* |
112
|
|
|
* @param array $arr The array to patch |
113
|
|
|
* @param array $patches List of new xpath-value pairs |
114
|
|
|
* @param string $parent |
115
|
|
|
* |
116
|
|
|
* @return array Returns patched array |
117
|
|
|
* |
118
|
|
|
*/ |
119
|
1 |
|
public static function patch($arr, $patches, $parent = '') |
120
|
|
|
{ |
121
|
1 |
|
foreach ($arr as $key => $value) { |
122
|
1 |
|
$z = $parent . '/' . $key; |
123
|
|
|
|
124
|
1 |
|
if (isset($patches[$z])) { |
125
|
1 |
|
$arr[$key] = $patches[$z]; |
126
|
1 |
|
unset($patches[$z]); |
127
|
|
|
|
128
|
1 |
|
if (!count($patches)) { |
129
|
1 |
|
break; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
if (is_array($value)) { |
134
|
1 |
|
$arr[$key] = static::patch($value, $patches, $z); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
return $arr; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* prend item or value to an array |
143
|
|
|
* |
144
|
|
|
** __::prepend([1, 2, 3], 4); |
145
|
|
|
** // → [4, 1, 2, 3] |
146
|
|
|
* |
147
|
|
|
* @param array $array |
148
|
|
|
* @param null $value |
|
|
|
|
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
* |
152
|
|
|
*/ |
153
|
2 |
|
public static function prepend(array $array = [], $value = null) |
154
|
|
|
{ |
155
|
2 |
|
\array_unshift($array, $value); |
156
|
|
|
|
157
|
2 |
|
return $array; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* generate range of values based on start , end and step |
162
|
|
|
** __::range(1, 10, 2); |
163
|
|
|
** // → [1, 3, 5, 7, 9] |
164
|
|
|
* |
165
|
|
|
* @param integer|null $start range start |
166
|
|
|
* @param integer|null $stop range end |
167
|
|
|
* @param integer $step range step value |
168
|
|
|
* |
169
|
|
|
* @return array range of values |
170
|
|
|
* |
171
|
|
|
*/ |
172
|
1 |
|
public static function range($start = null, $stop = null, $step = 1) |
173
|
|
|
{ |
174
|
1 |
|
if ($stop == null && $start != null) { |
|
|
|
|
175
|
1 |
|
$stop = $start; |
176
|
1 |
|
$start = 1; |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
return \range($start, $stop, $step); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* generate array of repeated values |
184
|
|
|
* |
185
|
|
|
** __::repeat('foo', 3); |
186
|
|
|
** // → ['foo', 'foo', 'foo'] |
187
|
|
|
* |
188
|
|
|
* @param string $object The object to repeat. |
189
|
|
|
* @param null $times ow many times has to be repeated. |
|
|
|
|
190
|
|
|
* |
191
|
|
|
* @return array Returns a new array of filled values. |
192
|
|
|
* |
193
|
|
|
*/ |
194
|
1 |
|
public static function repeat($object = '', $times = null) |
195
|
|
|
{ |
196
|
1 |
|
if ($times == null) { |
197
|
1 |
|
return []; |
198
|
|
|
} else { |
199
|
1 |
|
return \array_fill(0, $times, $object); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Creates an array of elements split into groups the length of size. If array can't be split evenly, the final |
206
|
|
|
* chunk will be the remaining elements. |
207
|
|
|
* |
208
|
|
|
* __::chunk([1, 2, 3, 4, 5], 3); |
209
|
|
|
* // → [[1, 2, 3], [4, 5]] |
210
|
|
|
* |
211
|
|
|
* @param array $array original array |
212
|
|
|
* @param int $size the chunk size |
213
|
|
|
* @param boolean $preserveKeys When set to TRUE keys will be preserved. Default is FALSE which will reindex the |
214
|
|
|
* chunk numerically |
215
|
|
|
* |
216
|
|
|
* @return array |
217
|
|
|
* |
218
|
|
|
*/ |
219
|
1 |
|
public static function chunk(array $array, $size = 1, $preserveKeys = false) |
220
|
|
|
{ |
221
|
1 |
|
return \array_chunk($array, $size, $preserveKeys); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Creates a slice of array with n elements dropped from the beginning. |
226
|
|
|
* |
227
|
|
|
** __::drop([0, 1, 3], 2); |
228
|
|
|
** // → [3] |
229
|
|
|
* |
230
|
|
|
* @param array $input The array to query. |
231
|
|
|
* @param int $number The number of elements to drop. |
232
|
|
|
* |
233
|
|
|
* @return array |
234
|
|
|
* |
235
|
|
|
*/ |
236
|
1 |
|
public static function drop(array $input, $number = 1) |
237
|
|
|
{ |
238
|
1 |
|
return array_slice($input, $number); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Shuffle an array ensuring no item remains in the same position. |
243
|
|
|
* |
244
|
|
|
** __::randomize([1, 2, 3]); |
245
|
|
|
** // → [2, 3, 1] |
246
|
|
|
* |
247
|
|
|
* @param array $array original array |
248
|
|
|
* |
249
|
|
|
* @return array |
250
|
|
|
* |
251
|
|
|
*/ |
252
|
1 |
|
public static function randomize(array $array) |
253
|
|
|
{ |
254
|
1 |
|
for ($i = 0, $c = \count($array); $i < $c - 1; $i++) { |
255
|
1 |
|
$j = \rand($i + 1, $c - 1); |
256
|
1 |
|
list($array[$i], $array[$j]) = [$array[$j], $array[$i]]; |
257
|
|
|
} |
258
|
|
|
|
259
|
1 |
|
return $array; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|