PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/obsolete/web_client/system/helpers/arr.php

http://github.com/gallery/gallery3-contrib
PHP | 275 lines | 139 code | 29 blank | 107 comment | 7 complexity | bd842571f55e014b8e37a69382a38c8c MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * Array helper class.
  4. *
  5. * $Id: arr.php 4680 2009-11-10 01:57:00Z isaiah $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2009 Kohana Team
  10. * @license http://kohanaphp.com/license
  11. */
  12. class arr_Core {
  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. $found[$key] = isset($search[$key]) ? $search[$key] : NULL;
  94. }
  95. return $found;
  96. }
  97. /**
  98. * Get the value of array[key]. If it doesn't exist, return default.
  99. *
  100. * @param array array to search
  101. * @param string key name
  102. * @param mixed default value
  103. * @return mixed
  104. */
  105. public static function get(array $array, $key, $default = NULL)
  106. {
  107. return isset($array[$key]) ? $array[$key] : $default;
  108. }
  109. /**
  110. * Because PHP does not have this function.
  111. *
  112. * @param array array to unshift
  113. * @param string key to unshift
  114. * @param mixed value to unshift
  115. * @return array
  116. */
  117. public static function unshift_assoc( array & $array, $key, $val)
  118. {
  119. $array = array_reverse($array, TRUE);
  120. $array[$key] = $val;
  121. $array = array_reverse($array, TRUE);
  122. return $array;
  123. }
  124. /**
  125. * Because PHP does not have this function, and array_walk_recursive creates
  126. * references in arrays and is not truly recursive.
  127. *
  128. * @param mixed callback to apply to each member of the array
  129. * @param array array to map to
  130. * @return array
  131. */
  132. public static function map_recursive($callback, array $array)
  133. {
  134. foreach ($array as $key => $val)
  135. {
  136. // Map the callback to the key
  137. $array[$key] = is_array($val) ? arr::map_recursive($callback, $val) : call_user_func($callback, $val);
  138. }
  139. return $array;
  140. }
  141. /**
  142. * Emulates array_merge_recursive, but appends numeric keys and replaces
  143. * associative keys, instead of appending all keys.
  144. *
  145. * @param array any number of arrays
  146. * @return array
  147. */
  148. public static function merge()
  149. {
  150. $total = func_num_args();
  151. $result = array();
  152. for ($i = 0; $i < $total; $i++)
  153. {
  154. foreach (func_get_arg($i) as $key => $val)
  155. {
  156. if (isset($result[$key]))
  157. {
  158. if (is_array($val))
  159. {
  160. // Arrays are merged recursively
  161. $result[$key] = arr::merge($result[$key], $val);
  162. }
  163. elseif (is_int($key))
  164. {
  165. // Indexed arrays are appended
  166. array_push($result, $val);
  167. }
  168. else
  169. {
  170. // Associative arrays are replaced
  171. $result[$key] = $val;
  172. }
  173. }
  174. else
  175. {
  176. // New values are added
  177. $result[$key] = $val;
  178. }
  179. }
  180. }
  181. return $result;
  182. }
  183. /**
  184. * Overwrites an array with values from input array(s).
  185. * Non-existing keys will not be appended!
  186. *
  187. * @param array key array
  188. * @param array input array(s) that will overwrite key array values
  189. * @return array
  190. */
  191. public static function overwrite($array1, $array2)
  192. {
  193. foreach (array_intersect_key($array2, $array1) as $key => $value)
  194. {
  195. $array1[$key] = $value;
  196. }
  197. if (func_num_args() > 2)
  198. {
  199. foreach (array_slice(func_get_args(), 2) as $array2)
  200. {
  201. foreach (array_intersect_key($array2, $array1) as $key => $value)
  202. {
  203. $array1[$key] = $value;
  204. }
  205. }
  206. }
  207. return $array1;
  208. }
  209. /**
  210. * Recursively convert an array to an object.
  211. *
  212. * @param array array to convert
  213. * @return object
  214. */
  215. public static function to_object(array $array, $class = 'stdClass')
  216. {
  217. $object = new $class;
  218. foreach ($array as $key => $value)
  219. {
  220. if (is_array($value))
  221. {
  222. // Convert the array to an object
  223. $value = arr::to_object($value, $class);
  224. }
  225. // Add the value to the object
  226. $object->{$key} = $value;
  227. }
  228. return $object;
  229. }
  230. /**
  231. * Returns specific key/column from an array of objects.
  232. *
  233. * @param string|integer $key The key or column number to pluck from each object.
  234. * @param array $array The array of objects to pluck from.
  235. * @return array
  236. */
  237. public static function pluck($key, $array)
  238. {
  239. $result = array();
  240. foreach ($array as $i => $object)
  241. {
  242. $result[$i] = isset($object[$key]) ? $object[$key] : NULL;
  243. }
  244. return $result;
  245. }
  246. } // End arr