PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/exido/helper/array.php

https://github.com/sharapovweb/exidoengine
PHP | 266 lines | 119 code | 25 blank | 122 comment | 17 complexity | 7ff11c50cd65faceb2435e7f1808afd7 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php defined('SYSPATH') or die('No direct script access allowed.');
  2. /*******************************************************************************
  3. * ExidoEngine Web-sites manager
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the GNU General Public License (3.0)
  8. * that is bundled with this package in the file license_en.txt
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.exidoengine.com/license/gpl-3.0.html
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@exidoengine.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade ExidoEngine to newer
  18. * versions in the future. If you wish to customize ExidoEngine for your
  19. * needs please refer to http://www.exidoengine.com for more information.
  20. *
  21. * @license http://www.exidoengine.com/license/gpl-3.0.html (GNU General Public License v3)
  22. * @author ExidoTeam
  23. * @copyright Copyright (c) 2009 - 2013, ExidoEngine Solutions
  24. * @link http://www.exidoengine.com/
  25. * @since Version 1.0
  26. * @filesource
  27. *******************************************************************************/
  28. /**
  29. * Convert to associative.
  30. * @param array $array
  31. * @return array
  32. */
  33. function arrayToAssoc(array $array)
  34. {
  35. $assoc = array();
  36. foreach($array as $data) {
  37. $assoc[reset($data)] = end($data);
  38. }
  39. return $assoc;
  40. }
  41. // -----------------------------------------------------------------------------
  42. /**
  43. * Checks if the input array is associative.
  44. * @param array $array
  45. * @return bool
  46. */
  47. function arrayAssoc(array $array)
  48. {
  49. $keys = array_keys($array);
  50. return array_keys($keys) !== $keys;
  51. }
  52. // -----------------------------------------------------------------------------
  53. /**
  54. * Gets the value using the "dot-noted" string.
  55. * @param array $array
  56. * @param string $path
  57. * @param null $default
  58. * @return null
  59. */
  60. function arrayPath($array, $path, $default = null)
  61. {
  62. // Split the string
  63. $keys = explode('.', $path);
  64. while($keys) {
  65. $key = array_shift($keys);
  66. if(ctype_digit($key)) {
  67. $key = (int)$key;
  68. }
  69. if(isset($array[$key])) {
  70. if($keys) {
  71. if(is_array($array[$key])) {
  72. // Got to next key
  73. $array = $array[$key];
  74. } else {
  75. // Key isn't found
  76. break;
  77. }
  78. } else {
  79. // Return founded value
  80. return $array[$key];
  81. }
  82. } else {
  83. // Key isn't found
  84. break;
  85. }
  86. }
  87. // Return default value
  88. return $default;
  89. }
  90. // -----------------------------------------------------------------------------
  91. /**
  92. * Returns an array with numbers.
  93. * @param int $step
  94. * @param int $max
  95. * @return array
  96. */
  97. function arrayRange($step = 10, $max = 100)
  98. {
  99. if($step < 1)
  100. return array();
  101. $array = array();
  102. for($i = $step; $i <= $max; $i += $step)
  103. $array[$i] = $i;
  104. return $array;
  105. }
  106. // -----------------------------------------------------------------------------
  107. /**
  108. * Returns an array with random numbers in specified range.
  109. * @param int $count
  110. * @param int $start
  111. * @param int $end
  112. * @return array
  113. */
  114. function arrayRandom($count = 100, $start = 0, $end = 10000)
  115. {
  116. $array = array();
  117. for($i = 0; $i < 200; $i++)
  118. $array[] = mt_rand(1, 1000);
  119. return $array;
  120. }
  121. // -----------------------------------------------------------------------------
  122. /**
  123. * Gets the key value. Return $default value if the key doesn't exists.
  124. * @param array $array
  125. * @param string $key
  126. * @param null $default
  127. * @return null
  128. */
  129. function arrayGet(array $array, $key, $default = null)
  130. {
  131. return isset($array[$key]) ? $array[$key] : $default;
  132. }
  133. // -----------------------------------------------------------------------------
  134. /**
  135. * Gets the array of key values. If one of the key does not found, returns $default
  136. * value for it.
  137. * @param array $array
  138. * @param array $keys
  139. * @param null $default
  140. * @return array
  141. */
  142. function arrayExtract(array $array, array $keys, $default = null)
  143. {
  144. $found = array();
  145. foreach($keys as $key)
  146. $found[$key] = isset($array[$key]) ? $array[$key] : $default;
  147. return $found;
  148. }
  149. // -----------------------------------------------------------------------------
  150. /**
  151. * Inserts value on the top of an array.
  152. * @param array $array
  153. * @param string $key
  154. * @param mixed $val
  155. * @return array
  156. */
  157. function arrayUnshift(array $array, $key, $val)
  158. {
  159. $array = array_reverse($array, true);
  160. $array[$key] = $val;
  161. $array = array_reverse($array, true);
  162. return $array;
  163. }
  164. // -----------------------------------------------------------------------------
  165. /**
  166. * Merges two or more arrays with recursive. This function isn't
  167. * a duplicate of array_merge_recursive()
  168. * @param array $a1
  169. * @param array ...
  170. * @return array
  171. */
  172. function arrayMerge(array $a1)
  173. {
  174. $result = array();
  175. for($i = 0, $total = func_num_args(); $i < $total; $i++) {
  176. foreach(func_get_arg($i) as $key => $val) {
  177. if(isset($result[$key])) {
  178. if(is_array($val)) {
  179. // Recursive merging
  180. $result[$key] = arrayMerge($result[$key], $val);
  181. } elseif(is_int($key)) {
  182. // Add value for indexed array
  183. array_push($result, $val);
  184. } else {
  185. // Replace value for associated array
  186. $result[$key] = $val;
  187. }
  188. } else {
  189. // Add a new values
  190. $result[$key] = $val;
  191. }
  192. }
  193. }
  194. return $result;
  195. }
  196. // -----------------------------------------------------------------------------
  197. /**
  198. * Strip slashes from array variables.
  199. * @param mixed $value
  200. * @return array|string
  201. */
  202. function arrayStripSlashes($value)
  203. {
  204. return is_array($value) ? array_map('arrayStripSlashes', $value) : stripslashes($value);
  205. }
  206. // -----------------------------------------------------------------------------
  207. /**
  208. * Replaces an array values with the values of another array.
  209. * @param array $array1
  210. * @param array $array2
  211. * @return array
  212. */
  213. function arrayOverwrite(array $array1, array $array2)
  214. {
  215. foreach(array_intersect_key($array2, $array1) as $key => $value) {
  216. $array1[$key] = $value;
  217. }
  218. if(func_num_args() > 2) {
  219. foreach(array_slice(func_get_args(), 2) as $array2) {
  220. foreach(array_intersect_key($array2, $array1) as $key => $value) {
  221. $array1[$key] = $value;
  222. }
  223. }
  224. }
  225. return $array1;
  226. }
  227. // -----------------------------------------------------------------------------
  228. /**
  229. * Remove array keys that have an empty values.
  230. * @param array $array
  231. * @return array
  232. */
  233. function arrayClean(array $array)
  234. {
  235. foreach($array as $key => $value) {
  236. if(empty($value))
  237. unset($array[$key]);
  238. }
  239. return $array;
  240. }
  241. ?>