Avail feature updated
[ninja.git] / system / helpers / arr.php
blobbc5d45c3bc21c322f6ee25df17322775c56056f2
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Array helper class.
5 * $Id: arr.php 3917 2009-01-21 03:06:22Z zombor $
7 * @package Core
8 * @author Kohana Team
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class arr {
14 /**
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
19 * @return array
21 public static function callback_string($str)
23 // command[param,param]
24 if (preg_match('/([^\[]*+)\[(.+)\]/', (string) $str, $match))
26 // command
27 $command = $match[1];
29 // param,param
30 $params = preg_split('/(?<!\\\\),/', $match[2]);
31 $params = str_replace('\,', ',', $params);
33 else
35 // command
36 $command = $str;
38 // No params
39 $params = NULL;
42 return array($command, $params);
45 /**
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!
52 * @return array
54 public static function rotate($source_array, $keep_keys = TRUE)
56 $new_array = array();
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;
66 return $new_array;
69 /**
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))
79 return NULL;
81 $val = $array[$key];
82 unset($array[$key]);
84 return $val;
88 /**
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
95 * @return array
97 public static function extract(array $search, $keys)
99 // Get the keys, removing the $search array
100 $keys = array_slice(func_get_args(), 1);
102 $found = array();
103 foreach ($keys as $key)
105 if (isset($search[$key]))
107 $found[$key] = $search[$key];
109 else
111 $found[$key] = NULL;
115 return $found;
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
124 * @return array
126 public static function unshift_assoc( array & $array, $key, $val)
128 $array = array_reverse($array, TRUE);
129 $array[$key] = $val;
130 $array = array_reverse($array, TRUE);
132 return $array;
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
141 * @return array
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);
151 return $array;
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
161 * @return integer
163 public static function binary_search($needle, $haystack, $nearest = FALSE, $sort = FALSE)
165 if ($sort === TRUE)
167 sort($haystack);
170 $high = count($haystack);
171 $low = 0;
173 while ($high - $low > 1)
175 $probe = ($high + $low) / 2;
176 if ($haystack[$probe] < $needle)
178 $low = $probe;
180 else
182 $high = $probe;
186 if ($high == count($haystack) OR $haystack[$high] != $needle)
188 if ($nearest === FALSE)
189 return 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)];
198 return $high;
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
206 * @return array
208 public static function merge()
210 $total = func_num_args();
212 $result = array();
213 for ($i = 0; $i < $total; $i++)
215 foreach (func_get_arg($i) as $key => $val)
217 if (isset($result[$key]))
219 if (is_array($val))
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);
229 else
231 // Associative arrays are replaced
232 $result[$key] = $val;
235 else
237 // New values are added
238 $result[$key] = $val;
243 return $result;
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
252 * @return array
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;
267 return $array1;
271 * Fill an array with a range of numbers.
273 * @param integer stepping
274 * @param integer ending number
275 * @return array
277 public static function range($step = 10, $max = 100)
279 if ($step < 1)
280 return array();
282 $array = array();
283 for ($i = $step; $i <= $max; $i += $step)
285 $array[$i] = $i;
288 return $array;
292 * Recursively convert an array to an object.
294 * @param array array to convert
295 * @return object
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;
313 return $object;
316 } // End arr