1
|
|
|
<?php |
2
|
|
|
|
3
|
|
View Code Duplication |
if (!function_exists('get')) { |
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Get an item from an array using "dot" notation. |
6
|
|
|
* |
7
|
|
|
* @param array $array |
8
|
|
|
* @param string $key |
9
|
|
|
* @param mixed $default |
10
|
|
|
* @return mixed |
11
|
|
|
*/ |
12
|
|
|
function get($array, $key, $default = null) |
13
|
|
|
{ |
14
|
|
|
if (is_null($key)) { |
15
|
|
|
return $array; |
16
|
|
|
} |
17
|
|
|
if (isset($array[$key])) { |
18
|
|
|
return $array[$key]; |
19
|
|
|
} |
20
|
|
|
foreach (explode('.', $key) as $segment) { |
21
|
|
|
if (!is_array($array) || !array_key_exists($segment, $array)) { |
22
|
|
|
return value($default); |
23
|
|
|
} |
24
|
|
|
$array = $array[$segment]; |
25
|
|
|
} |
26
|
|
|
return $array; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (!function_exists('set')) { |
31
|
|
|
/** |
32
|
|
|
* Set an array item to a given value using "dot" notation. |
33
|
|
|
* |
34
|
|
|
* If no key is given to the method, the entire array will be replaced. |
35
|
|
|
* |
36
|
|
|
* @param array $array |
37
|
|
|
* @param string $key |
38
|
|
|
* @param mixed $value |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
function set(&$array, $key, $value) |
42
|
|
|
{ |
43
|
|
|
if (is_null($key)) { |
44
|
|
|
return $array = $value; |
45
|
|
|
} |
46
|
|
|
$keys = explode('.', $key); |
47
|
|
|
while (count($keys) > 1) { |
48
|
|
|
$key = array_shift($keys); |
49
|
|
|
// If the key doesn't exist at this depth, we will just create an empty array |
50
|
|
|
// to hold the next value, allowing us to create the arrays to hold final |
51
|
|
|
// values at the correct depth. Then we'll keep digging into the array. |
52
|
|
|
if (!isset($array[$key]) || !is_array($array[$key])) { |
53
|
|
|
$array[$key] = array(); |
54
|
|
|
} |
55
|
|
|
$array =& $array[$key]; |
56
|
|
|
} |
57
|
|
|
$array[array_shift($keys)] = $value; |
58
|
|
|
return $array; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!function_exists('head')) { |
63
|
|
|
/** |
64
|
|
|
* Get the first element of an array. Useful for method chaining. |
65
|
|
|
* |
66
|
|
|
* @param array $array |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
function head($array) |
70
|
|
|
{ |
71
|
|
|
return reset($array); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (!function_exists('last')) { |
76
|
|
|
/** |
77
|
|
|
* Get the last element from an array. |
78
|
|
|
* |
79
|
|
|
* @param array $array |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
function last($array) |
83
|
|
|
{ |
84
|
|
|
return end($array); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!function_exists('array_has')) { |
89
|
|
|
/** |
90
|
|
|
* Check if an item exists in an array using "dot" notation. |
91
|
|
|
* |
92
|
|
|
* @param array $array |
93
|
|
|
* @param string $key |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
function array_has($array, $key) |
97
|
|
|
{ |
98
|
|
|
if (empty($array) || is_null($key)) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
if (array_key_exists($key, $array)) { |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
foreach (explode('.', $key) as $segment) { |
105
|
|
|
if (!is_array($array) || !array_key_exists($segment, $array)) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
$array = $array[$segment]; |
109
|
|
|
} |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
View Code Duplication |
if (!function_exists('array_get')) { |
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get an item from an array using "dot" notation. |
117
|
|
|
* |
118
|
|
|
* @param array $array |
119
|
|
|
* @param string $key |
120
|
|
|
* @param mixed $default |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
|
|
function array_get($array, $key, $default = null) |
124
|
|
|
{ |
125
|
|
|
if (is_null($key)) { |
126
|
|
|
return $array; |
127
|
|
|
} |
128
|
|
|
if (isset($array[$key])) { |
129
|
|
|
return $array[$key]; |
130
|
|
|
} |
131
|
|
|
foreach (explode('.', $key) as $segment) { |
132
|
|
|
if (!is_array($array) || !array_key_exists($segment, $array)) { |
133
|
|
|
return value($default); |
134
|
|
|
} |
135
|
|
|
$array = $array[$segment]; |
136
|
|
|
} |
137
|
|
|
return $array; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return an array with only integers value contained in the array passed |
143
|
|
|
* @param array $array |
144
|
|
|
* @return array |
145
|
|
|
**/ |
146
|
|
|
function CleanUpArrayOfInt($array) |
147
|
|
|
{ |
148
|
|
|
$result = array(); |
149
|
|
|
if (!is_array($array) || count($array) < 1) { |
150
|
|
|
return $result; |
151
|
|
|
} |
152
|
|
|
reset($array); |
153
|
|
|
while (list($key, $value) = each($array)) { |
|
|
|
|
154
|
|
|
if (isInteger($value)) { |
155
|
|
|
$result[] = $value; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
reset($array); |
159
|
|
|
|
160
|
|
|
return $result; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns an array with two elements. |
165
|
|
|
* |
166
|
|
|
* Iterates over each value in the array passing them to the callback function. |
167
|
|
|
* If the callback function returns true, the current value from array is returned in the first |
168
|
|
|
* element of result array. If not, it is return in the second element of result array. |
169
|
|
|
* |
170
|
|
|
* Array keys are preserved. |
171
|
|
|
* |
172
|
|
|
* @param array $array |
173
|
|
|
* @param callable $callback |
174
|
|
|
* @return array |
175
|
|
|
* @see https://github.com/spatie/array-functions/blob/master/src/array_functions.php |
176
|
|
|
*/ |
177
|
|
|
function array_split_filter(array $array, callable $callback) |
178
|
|
|
{ |
179
|
|
|
$passesFilter = array_filter($array, $callback); |
180
|
|
|
$negatedCallback = function ($item) use ($callback) { |
181
|
|
|
return !$callback($item); |
182
|
|
|
}; |
183
|
|
|
$doesNotPassFilter = array_filter($array, $negatedCallback); |
184
|
|
|
return [$passesFilter, $doesNotPassFilter]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Checks whether specific value exists in array of object. |
189
|
|
|
* For exampe, following code |
190
|
|
|
* $exist = in_array_column([['id' => 1], ['id' => 2], ['id' => 3]], 3, 'id'); |
191
|
|
|
* will produce 2 |
192
|
|
|
* @author wapmorgan |
193
|
|
|
* @since 2015.05.19 |
194
|
|
|
* @param array $haystack Source array |
195
|
|
|
* @param mixed $needle Needed value |
196
|
|
|
* @param string $column Column to perform search |
197
|
|
|
* @param bool $strict Should search be strict or not. |
198
|
|
|
* @return bool True if value exists in array, False otherwise. |
199
|
|
|
* @see modified from https://github.com/wapmorgan/php-functions-repository/blob/master/i/in_array_column.php |
200
|
|
|
*/ |
201
|
|
|
function in_array_column($haystack, $needle, $column, $strict = false) |
202
|
|
|
{ |
203
|
|
|
foreach ($haystack as $k => $elem) { |
204
|
|
|
if ((!$strict && $elem[$column] == $needle) || ($strict && $elem[$column] === $needle)) { |
205
|
|
|
return true; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.