|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Support; |
|
4
|
|
|
|
|
5
|
|
|
class Arr |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Get a random value from an array. |
|
9
|
|
|
* |
|
10
|
|
|
* @param array $array |
|
11
|
|
|
* @param int $numReq The amount of values to return |
|
12
|
|
|
* |
|
13
|
|
|
* @return mixed |
|
14
|
|
|
*/ |
|
15
|
|
|
public static function randValue(array $array, $numReq = 1) |
|
16
|
|
|
{ |
|
17
|
|
|
if (!count($array)) { |
|
18
|
|
|
return; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
$keys = array_rand($array, $numReq); |
|
22
|
|
|
|
|
23
|
|
|
if ($numReq === 1) { |
|
24
|
|
|
return $array[$keys]; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return array_intersect_key($array, array_flip($keys)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get a random value from an array, with the ability to skew the results. |
|
32
|
|
|
* Example: Arr::randWeighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $array |
|
35
|
|
|
* |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function randWeighted(array $array) |
|
39
|
|
|
{ |
|
40
|
|
|
$options = []; |
|
41
|
|
|
|
|
42
|
|
|
foreach ($array as $option => $weight) { |
|
43
|
|
|
for ($i = 0; $i < $weight; ++$i) { |
|
44
|
|
|
$options[] = $option; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return array_rand_value($options); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Determine if all given needles are present in the haystack. |
|
53
|
|
|
* |
|
54
|
|
|
* @param array|string $needles |
|
55
|
|
|
* @param array $haystack |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function valuesInArray($needles, array $haystack) |
|
60
|
|
|
{ |
|
61
|
|
|
if (!is_array($needles)) { |
|
62
|
|
|
$needles = [$needles]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return count(array_intersect($needles, $haystack)) === count($needles); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Determine if all given needles are present in the haystack as array keys. |
|
70
|
|
|
* |
|
71
|
|
|
* @param array|string $needles |
|
72
|
|
|
* @param array $haystack |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function keysExist($needles, array $haystack) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!is_array($needles)) { |
|
79
|
|
|
return array_key_exists($needles, $haystack); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return values_in_array($needles, array_keys($haystack)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns an array with two elements. |
|
87
|
|
|
* |
|
88
|
|
|
* Iterates over each value in the array passing them to the callback function. |
|
89
|
|
|
* If the callback function returns true, the current value from array is returned in the first |
|
90
|
|
|
* element of result array. If not, it is return in the second element of result array. |
|
91
|
|
|
* |
|
92
|
|
|
* Array keys are preserved. |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $array |
|
95
|
|
|
* @param callable $callback |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function splitFilter(array $array, callable $callback) |
|
100
|
|
|
{ |
|
101
|
|
|
$passesFilter = array_filter($array, $callback); |
|
102
|
|
|
|
|
103
|
|
|
$negatedCallback = static function ($item) use ($callback) {return !$callback($item);}; |
|
104
|
|
|
|
|
105
|
|
|
$doesNotPassFilter = array_filter($array, $negatedCallback); |
|
106
|
|
|
|
|
107
|
|
|
return [$passesFilter, $doesNotPassFilter]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Split an array in the given amount of pieces. |
|
112
|
|
|
* |
|
113
|
|
|
* @param array $array |
|
114
|
|
|
* @param int $numberOfPieces |
|
115
|
|
|
* @param bool $preserveKeys |
|
116
|
|
|
* @throws \InvalidArgumentException if the provided argument $numberOfPieces is lower than 1 |
|
117
|
|
|
* |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
|
|
public static function split(array $array, $numberOfPieces = 2, $preserveKeys = false) |
|
121
|
|
|
{ |
|
122
|
|
|
if ($numberOfPieces <= 0) { |
|
123
|
|
|
throw new \InvalidArgumentException('Number of pieces parameter expected to be greater than 0'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if (count($array) === 0) { |
|
127
|
|
|
return []; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$splitSize = ceil(count($array) / $numberOfPieces); |
|
131
|
|
|
|
|
132
|
|
|
return array_chunk($array, $splitSize, $preserveKeys); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns an array with the unique values from all the given arrays. |
|
137
|
|
|
* |
|
138
|
|
|
* @param \array[] $arrays |
|
139
|
|
|
* |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function mergeValues(array...$arrays) |
|
143
|
|
|
{ |
|
144
|
|
|
$allValues = array_reduce($arrays, static function ($carry, $array) { |
|
145
|
|
|
return array_merge($carry, $array); |
|
146
|
|
|
}, []); |
|
147
|
|
|
|
|
148
|
|
|
return array_values(array_unique($allValues)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Flatten an array of arrays. The `$levels` parameter specifies how deep you want to |
|
153
|
|
|
* recurse in the array. If `$levels` is -1, the function will recurse infinitely. |
|
154
|
|
|
* |
|
155
|
|
|
* @param array $array |
|
156
|
|
|
* @param int $levels |
|
157
|
|
|
* |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
public static function flatten(array $array, $levels = -1) |
|
161
|
|
|
{ |
|
162
|
|
|
if ($levels === 0) { |
|
163
|
|
|
return $array; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$flattened = []; |
|
167
|
|
|
|
|
168
|
|
|
if ($levels !== -1) { |
|
169
|
|
|
--$levels; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
foreach ($array as $element) { |
|
173
|
|
|
$flattened[] = is_array($element) ? array_flatten($element, $levels) : [$element]; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return array_merge([], ...$flattened); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|