|
1
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.'); |
|
2
|
|
|
/** |
|
3
|
|
|
* Array helper class. |
|
4
|
|
|
* |
|
5
|
|
|
* $Id: arr.php 4346 2009-05-11 17:08:15Z zombor $ |
|
6
|
|
|
* |
|
7
|
|
|
* @package Core |
|
8
|
|
|
* @author Kohana Team |
|
9
|
|
|
* @copyright (c) 2007-2008 Kohana Team |
|
10
|
|
|
* @license http://kohanaphp.com/license.html |
|
11
|
|
|
*/ |
|
12
|
|
|
class arr_Core |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Return a callback array from a string, eg: limit[10,20] would become |
|
17
|
|
|
* array('limit', array('10', '20')) |
|
18
|
|
|
* |
|
19
|
|
|
* @param string callback string |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function callback_string($str) |
|
23
|
|
|
{ |
|
24
|
|
|
// command[param,param] |
|
25
|
|
|
if (preg_match('/([^\[]*+)\[(.+)\]/', (string) $str, $match)) { |
|
26
|
|
|
// command |
|
27
|
|
|
$command = $match[1]; |
|
28
|
|
|
|
|
29
|
|
|
// param,param |
|
30
|
|
|
$params = preg_split('/(?<!\\\\),/', $match[2]); |
|
31
|
|
|
$params = str_replace('\,', ',', $params); |
|
32
|
|
|
} else { |
|
33
|
|
|
// command |
|
34
|
|
|
$command = $str; |
|
35
|
|
|
|
|
36
|
|
|
// No params |
|
37
|
|
|
$params = null; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return array($command, $params); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Rotates a 2D array clockwise. |
|
45
|
|
|
* Example, turns a 2x3 array into a 3x2 array. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array array to rotate |
|
48
|
|
|
* @param boolean keep the keys in the final rotated array. the sub arrays of the source array need to have the same key values. |
|
49
|
|
|
* if your subkeys might not match, you need to pass FALSE here! |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function rotate($source_array, $keep_keys = true) |
|
53
|
|
|
{ |
|
54
|
|
|
$new_array = array(); |
|
55
|
|
|
foreach ($source_array as $key => $value) { |
|
56
|
|
|
$value = ($keep_keys === true) ? $value : array_values($value); |
|
57
|
|
|
foreach ($value as $k => $v) { |
|
58
|
|
|
$new_array[$k][$key] = $v; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $new_array; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Removes a key from an array and returns the value. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string key to return |
|
69
|
|
|
* @param array array to work on |
|
70
|
|
|
* @return mixed value of the requested array key |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function remove($key, & $array) |
|
73
|
|
|
{ |
|
74
|
|
|
if (! array_key_exists($key, $array)) { |
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$val = $array[$key]; |
|
79
|
|
|
unset($array[$key]); |
|
80
|
|
|
|
|
81
|
|
|
return $val; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Extract one or more keys from an array. Each key given after the first |
|
87
|
|
|
* argument (the array) will be extracted. Keys that do not exist in the |
|
88
|
|
|
* search array will be NULL in the extracted data. |
|
89
|
|
|
* |
|
90
|
|
|
* @param array array to search |
|
91
|
|
|
* @param string key name |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function extract(array $search, $keys) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
// Get the keys, removing the $search array |
|
97
|
|
|
$keys = array_slice(func_get_args(), 1); |
|
98
|
|
|
|
|
99
|
|
|
$found = array(); |
|
100
|
|
|
foreach ($keys as $key) { |
|
101
|
|
|
if (isset($search[$key])) { |
|
102
|
|
|
$found[$key] = $search[$key]; |
|
103
|
|
|
} else { |
|
104
|
|
|
$found[$key] = null; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $found; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Because PHP does not have this function. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array array to unshift |
|
115
|
|
|
* @param string key to unshift |
|
116
|
|
|
* @param mixed value to unshift |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function unshift_assoc(array & $array, $key, $val) |
|
120
|
|
|
{ |
|
121
|
|
|
$array = array_reverse($array, true); |
|
122
|
|
|
$array[$key] = $val; |
|
123
|
|
|
$array = array_reverse($array, true); |
|
124
|
|
|
|
|
125
|
|
|
return $array; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Because PHP does not have this function, and array_walk_recursive creates |
|
130
|
|
|
* references in arrays and is not truly recursive. |
|
131
|
|
|
* |
|
132
|
|
|
* @param mixed callback to apply to each member of the array |
|
133
|
|
|
* @param array array to map to |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
|
|
public static function map_recursive($callback, array $array) |
|
137
|
|
|
{ |
|
138
|
|
|
foreach ($array as $key => $val) { |
|
139
|
|
|
// Map the callback to the key |
|
140
|
|
|
$array[$key] = is_array($val) ? arr::map_recursive($callback, $val) : call_user_func($callback, $val); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $array; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param mixed $needle the value to search for |
|
148
|
|
|
* @param array $haystack an array of values to search in |
|
149
|
|
|
* @param boolean $sort sort the array now |
|
150
|
|
|
* @return integer|FALSE the index of the match or FALSE when not found |
|
151
|
|
|
*/ |
|
152
|
|
|
public static function binary_search($needle, $haystack, $sort = false) |
|
153
|
|
|
{ |
|
154
|
|
|
if ($sort) { |
|
155
|
|
|
sort($haystack); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$high = count($haystack) - 1; |
|
159
|
|
|
$low = 0; |
|
160
|
|
|
|
|
161
|
|
|
while ($low <= $high) { |
|
162
|
|
|
$mid = ($low + $high) >> 1; |
|
163
|
|
|
|
|
164
|
|
|
if ($haystack[$mid] < $needle) { |
|
165
|
|
|
$low = $mid + 1; |
|
166
|
|
|
} elseif ($haystack[$mid] > $needle) { |
|
167
|
|
|
$high = $mid - 1; |
|
168
|
|
|
} else { |
|
169
|
|
|
return $mid; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Emulates array_merge_recursive, but appends numeric keys and replaces |
|
179
|
|
|
* associative keys, instead of appending all keys. |
|
180
|
|
|
* |
|
181
|
|
|
* @param array any number of arrays |
|
182
|
|
|
* @return array |
|
183
|
|
|
*/ |
|
184
|
|
|
public static function merge() |
|
185
|
|
|
{ |
|
186
|
|
|
$total = func_num_args(); |
|
187
|
|
|
|
|
188
|
|
|
$result = array(); |
|
189
|
|
|
for ($i = 0; $i < $total; $i++) { |
|
190
|
|
|
foreach (func_get_arg($i) as $key => $val) { |
|
191
|
|
|
if (isset($result[$key])) { |
|
192
|
|
|
if (is_array($val)) { |
|
193
|
|
|
// Arrays are merged recursively |
|
194
|
|
|
$result[$key] = arr::merge($result[$key], $val); |
|
195
|
|
|
} elseif (is_int($key)) { |
|
196
|
|
|
// Indexed arrays are appended |
|
197
|
|
|
array_push($result, $val); |
|
198
|
|
|
} else { |
|
199
|
|
|
// Associative arrays are replaced |
|
200
|
|
|
$result[$key] = $val; |
|
201
|
|
|
} |
|
202
|
|
|
} else { |
|
203
|
|
|
// New values are added |
|
204
|
|
|
$result[$key] = $val; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $result; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Overwrites an array with values from input array(s). |
|
214
|
|
|
* Non-existing keys will not be appended! |
|
215
|
|
|
* |
|
216
|
|
|
* @param array key array |
|
217
|
|
|
* @param array input array(s) that will overwrite key array values |
|
218
|
|
|
* @return array |
|
219
|
|
|
*/ |
|
220
|
|
|
public static function overwrite($array1, $array2) |
|
221
|
|
|
{ |
|
222
|
|
|
foreach (array_intersect_key($array2, $array1) as $key => $value) { |
|
223
|
|
|
$array1[$key] = $value; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
if (func_num_args() > 2) { |
|
227
|
|
|
foreach (array_slice(func_get_args(), 2) as $array2) { |
|
228
|
|
|
foreach (array_intersect_key($array2, $array1) as $key => $value) { |
|
229
|
|
|
$array1[$key] = $value; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return $array1; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Fill an array with a range of numbers. |
|
239
|
|
|
* |
|
240
|
|
|
* @param integer stepping |
|
241
|
|
|
* @param integer ending number |
|
242
|
|
|
* @return array |
|
243
|
|
|
*/ |
|
244
|
|
|
public static function range($step = 10, $max = 100) |
|
245
|
|
|
{ |
|
246
|
|
|
if ($step < 1) { |
|
247
|
|
|
return array(); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
$array = array(); |
|
251
|
|
|
for ($i = $step; $i <= $max; $i += $step) { |
|
252
|
|
|
$array[$i] = $i; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
return $array; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Recursively convert an array to an object. |
|
260
|
|
|
* |
|
261
|
|
|
* @param array array to convert |
|
262
|
|
|
* @return object |
|
263
|
|
|
*/ |
|
264
|
|
|
public static function to_object(array $array, $class = 'stdClass') |
|
265
|
|
|
{ |
|
266
|
|
|
$object = new $class; |
|
267
|
|
|
|
|
268
|
|
|
foreach ($array as $key => $value) { |
|
269
|
|
|
if (is_array($value)) { |
|
270
|
|
|
// Convert the array to an object |
|
271
|
|
|
$value = arr::to_object($value, $class); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
// Add the value to the object |
|
275
|
|
|
$object->{$key} = $value; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
return $object; |
|
279
|
|
|
} |
|
280
|
|
|
} // End arr |
|
281
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.