1 <?php defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: arr.php 4346 2009-05-11 17:08:15Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
15 * Return a callback array from a string, eg: limit[10,20] would become
16 * array('limit', array('10', '20'))
18 * @param string callback string
21 public static function callback_string($str)
23 // command[param,param]
24 if (preg_match('/([^\[]*+)\[(.+)\]/', (string) $str, $match))
30 $params = preg_split('/(?<!\\\\),/', $match[2]);
31 $params = str_replace('\,', ',', $params);
42 return array($command, $params);
46 * Rotates a 2D array clockwise.
47 * Example, turns a 2x3 array into a 3x2 array.
49 * @param array array to rotate
50 * @param boolean keep the keys in the final rotated array. the sub arrays of the source array need to have the same key values.
51 * if your subkeys might not match, you need to pass FALSE here!
54 public static function rotate($source_array, $keep_keys = TRUE)
57 foreach ($source_array as $key => $value)
59 $value = ($keep_keys === TRUE) ? $value : array_values($value);
60 foreach ($value as $k => $v)
62 $new_array[$k][$key] = $v;
70 * Removes a key from an array and returns the value.
72 * @param string key to return
73 * @param array array to work on
74 * @return mixed value of the requested array key
76 public static function remove($key, & $array)
78 if ( ! array_key_exists($key, $array))
89 * Extract one or more keys from an array. Each key given after the first
90 * argument (the array) will be extracted. Keys that do not exist in the
91 * search array will be NULL in the extracted data.
93 * @param array array to search
94 * @param string key name
97 public static function extract(array $search, $keys)
99 // Get the keys, removing the $search array
100 $keys = array_slice(func_get_args(), 1);
103 foreach ($keys as $key)
105 if (isset($search[$key]))
107 $found[$key] = $search[$key];
119 * Because PHP does not have this function.
121 * @param array array to unshift
122 * @param string key to unshift
123 * @param mixed value to unshift
126 public static function unshift_assoc( array & $array, $key, $val)
128 $array = array_reverse($array, TRUE);
130 $array = array_reverse($array, TRUE);
136 * Because PHP does not have this function, and array_walk_recursive creates
137 * references in arrays and is not truly recursive.
139 * @param mixed callback to apply to each member of the array
140 * @param array array to map to
143 public static function map_recursive($callback, array $array)
145 foreach ($array as $key => $val)
147 // Map the callback to the key
148 $array[$key] = is_array($val) ? arr::map_recursive($callback, $val) : call_user_func($callback, $val);
155 * @param mixed $needle the value to search for
156 * @param array $haystack an array of values to search in
157 * @param boolean $sort sort the array now
158 * @return integer|FALSE the index of the match or FALSE when not found
160 public static function binary_search($needle, $haystack, $sort = FALSE)
167 $high = count($haystack) - 1;
170 while ($low <= $high)
172 $mid = ($low + $high) >> 1;
174 if ($haystack[$mid] < $needle)
178 elseif ($haystack[$mid] > $needle)
193 * Emulates array_merge_recursive, but appends numeric keys and replaces
194 * associative keys, instead of appending all keys.
196 * @param array any number of arrays
199 public static function merge()
201 $total = func_num_args();
204 for ($i = 0; $i < $total; $i++)
206 foreach (func_get_arg($i) as $key => $val)
208 if (isset($result[$key]))
212 // Arrays are merged recursively
213 $result[$key] = arr::merge($result[$key], $val);
215 elseif (is_int($key))
217 // Indexed arrays are appended
218 array_push($result, $val);
222 // Associative arrays are replaced
223 $result[$key] = $val;
228 // New values are added
229 $result[$key] = $val;
238 * Overwrites an array with values from input array(s).
239 * Non-existing keys will not be appended!
241 * @param array key array
242 * @param array input array(s) that will overwrite key array values
245 public static function overwrite($array1, $array2)
247 foreach (array_intersect_key($array2, $array1) as $key => $value)
249 $array1[$key] = $value;
252 if (func_num_args() > 2)
254 foreach (array_slice(func_get_args(), 2) as $array2)
256 foreach (array_intersect_key($array2, $array1) as $key => $value)
258 $array1[$key] = $value;
267 * Fill an array with a range of numbers.
269 * @param integer stepping
270 * @param integer ending number
273 public static function range($step = 10, $max = 100)
279 for ($i = $step; $i <= $max; $i += $step)
288 * Recursively convert an array to an object.
290 * @param array array to convert
293 public static function to_object(array $array, $class = 'stdClass')
295 $object = new $class;
297 foreach ($array as $key => $value)
299 if (is_array($value))
301 // Convert the array to an object
302 $value = arr::to_object($value, $class);
305 // Add the value to the object
306 $object->{$key} = $value;