1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: arr.php 3917 2009-01-21 03:06:22Z 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 * Binary search algorithm.
157 * @param mixed the value to search for
158 * @param array an array of values to search in
159 * @param boolean return false, or the nearest value
160 * @param mixed sort the array before searching it
163 public static function binary_search($needle, $haystack, $nearest = FALSE, $sort = FALSE)
170 $high = count($haystack);
173 while ($high - $low > 1)
175 $probe = ($high +
$low) / 2;
176 if ($haystack[$probe] < $needle)
186 if ($high == count($haystack) OR $haystack[$high] != $needle)
188 if ($nearest === FALSE)
191 // return the nearest value
192 $high_distance = $haystack[ceil($low)] - $needle;
193 $low_distance = $needle - $haystack[floor($low)];
195 return ($high_distance >= $low_distance) ?
$haystack[ceil($low)] : $haystack[floor($low)];
202 * Emulates array_merge_recursive, but appends numeric keys and replaces
203 * associative keys, instead of appending all keys.
205 * @param array any number of arrays
208 public static function merge()
210 $total = func_num_args();
213 for ($i = 0; $i < $total; $i++
)
215 foreach (func_get_arg($i) as $key => $val)
217 if (isset($result[$key]))
221 // Arrays are merged recursively
222 $result[$key] = arr
::merge($result[$key], $val);
224 elseif (is_int($key))
226 // Indexed arrays are appended
227 array_push($result, $val);
231 // Associative arrays are replaced
232 $result[$key] = $val;
237 // New values are added
238 $result[$key] = $val;
247 * Overwrites an array with values from input array(s).
248 * Non-existing keys will not be appended!
250 * @param array key array
251 * @param array input array(s) that will overwrite key array values
254 public static function overwrite($array1)
256 foreach (array_slice(func_get_args(), 1) as $array2)
258 foreach ($array2 as $key => $value)
260 if (array_key_exists($key, $array1))
262 $array1[$key] = $value;
271 * Fill an array with a range of numbers.
273 * @param integer stepping
274 * @param integer ending number
277 public static function range($step = 10, $max = 100)
283 for ($i = $step; $i <= $max; $i +
= $step)
292 * Recursively convert an array to an object.
294 * @param array array to convert
297 public static function to_object(array $array, $class = 'stdClass')
299 $object = new $class;
301 foreach ($array as $key => $value)
303 if (is_array($value))
305 // Convert the array to an object
306 $value = arr
::to_object($value, $class);
309 // Add the value to the object
310 $object->{$key} = $value;