PageRenderTime 71ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/protected/helpers/CArray.php

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