/libraries/Zend/Stdlib/ArrayUtils.php

https://github.com/kiranatama/sagalaya · PHP · 248 lines · 105 code · 28 blank · 115 comment · 23 complexity · a6fd561fb92871c72e995419730941cc MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Stdlib
  9. */
  10. namespace Zend\Stdlib;
  11. use Traversable;
  12. /**
  13. * Utility class for testing and manipulation of PHP arrays.
  14. *
  15. * Declared abstract, as we have no need for instantiation.
  16. *
  17. * @category Zend
  18. * @package Zend_Stdlib
  19. */
  20. abstract class ArrayUtils
  21. {
  22. /**
  23. * Test whether an array contains one or more string keys
  24. *
  25. * @param mixed $value
  26. * @param bool $allowEmpty Should an empty array() return true
  27. * @return bool
  28. */
  29. public static function hasStringKeys($value, $allowEmpty = false)
  30. {
  31. if (!is_array($value)) {
  32. return false;
  33. }
  34. if (!$value) {
  35. return $allowEmpty;
  36. }
  37. return count(array_filter(array_keys($value), 'is_string')) > 0;
  38. }
  39. /**
  40. * Test whether an array contains one or more integer keys
  41. *
  42. * @param mixed $value
  43. * @param bool $allowEmpty Should an empty array() return true
  44. * @return bool
  45. */
  46. public static function hasIntegerKeys($value, $allowEmpty = false)
  47. {
  48. if (!is_array($value)) {
  49. return false;
  50. }
  51. if (!$value) {
  52. return $allowEmpty;
  53. }
  54. return count(array_filter(array_keys($value), 'is_int')) > 0;
  55. }
  56. /**
  57. * Test whether an array contains one or more numeric keys.
  58. *
  59. * A numeric key can be one of the following:
  60. * - an integer 1,
  61. * - a string with a number '20'
  62. * - a string with negative number: '-1000'
  63. * - a float: 2.2120, -78.150999
  64. * - a string with float: '4000.99999', '-10.10'
  65. *
  66. * @param mixed $value
  67. * @param bool $allowEmpty Should an empty array() return true
  68. * @return bool
  69. */
  70. public static function hasNumericKeys($value, $allowEmpty = false)
  71. {
  72. if (!is_array($value)) {
  73. return false;
  74. }
  75. if (!$value) {
  76. return $allowEmpty;
  77. }
  78. return count(array_filter(array_keys($value), 'is_numeric')) > 0;
  79. }
  80. /**
  81. * Test whether an array is a list
  82. *
  83. * A list is a collection of values assigned to continuous integer keys
  84. * starting at 0 and ending at count() - 1.
  85. *
  86. * For example:
  87. * <code>
  88. * $list = array( 'a','b','c','d' );
  89. * $list = array(
  90. * 0 => 'foo',
  91. * 1 => 'bar',
  92. * 2 => array( 'foo' => 'baz' ),
  93. * );
  94. * </code>
  95. *
  96. * @param mixed $value
  97. * @param bool $allowEmpty Is an empty list a valid list?
  98. * @return bool
  99. */
  100. public static function isList($value, $allowEmpty = false)
  101. {
  102. if (!is_array($value)) {
  103. return false;
  104. }
  105. if (!$value) {
  106. return $allowEmpty;
  107. }
  108. return (array_values($value) === $value);
  109. }
  110. /**
  111. * Test whether an array is a hash table.
  112. *
  113. * An array is a hash table if:
  114. *
  115. * 1. Contains one or more non-integer keys, or
  116. * 2. Integer keys are non-continuous or misaligned (not starting with 0)
  117. *
  118. * For example:
  119. * <code>
  120. * $hash = array(
  121. * 'foo' => 15,
  122. * 'bar' => false,
  123. * );
  124. * $hash = array(
  125. * 1995 => 'Birth of PHP',
  126. * 2009 => 'PHP 5.3.0',
  127. * 2012 => 'PHP 5.4.0',
  128. * );
  129. * $hash = array(
  130. * 'formElement,
  131. * 'options' => array( 'debug' => true ),
  132. * );
  133. * </code>
  134. *
  135. * @param mixed $value
  136. * @param bool $allowEmpty Is an empty array() a valid hash table?
  137. * @return bool
  138. */
  139. public static function isHashTable($value, $allowEmpty = false)
  140. {
  141. if (!is_array($value)) {
  142. return false;
  143. }
  144. if (!$value) {
  145. return $allowEmpty;
  146. }
  147. return (array_values($value) !== $value);
  148. }
  149. /**
  150. * Convert an iterator to an array.
  151. *
  152. * Converts an iterator to an array. The $recursive flag, on by default,
  153. * hints whether or not you want to do so recursively.
  154. *
  155. * @param array|Traversable $iterator The array or Traversable object to convert
  156. * @param bool $recursive Recursively check all nested structures
  157. * @return array
  158. */
  159. public static function iteratorToArray($iterator, $recursive = true)
  160. {
  161. if (!is_array($iterator) && !$iterator instanceof Traversable) {
  162. throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable object');
  163. }
  164. if (!$recursive) {
  165. if (is_array($iterator)) {
  166. return $iterator;
  167. }
  168. return iterator_to_array($iterator);
  169. }
  170. if (method_exists($iterator, 'toArray')) {
  171. return $iterator->toArray();
  172. }
  173. $array = array();
  174. foreach ($iterator as $key => $value) {
  175. if (is_scalar($value)) {
  176. $array[$key] = $value;
  177. continue;
  178. }
  179. if ($value instanceof Traversable) {
  180. $array[$key] = static::iteratorToArray($value, $recursive);
  181. continue;
  182. }
  183. if (is_array($value)) {
  184. $array[$key] = static::iteratorToArray($value, $recursive);
  185. continue;
  186. }
  187. $array[$key] = $value;
  188. }
  189. return $array;
  190. }
  191. /**
  192. * Merge two arrays together.
  193. *
  194. * If an integer key exists in both arrays, the value from the second array
  195. * will be appended the the first array. If both values are arrays, they
  196. * are merged together, else the value of the second array overwrites the
  197. * one of the first array.
  198. *
  199. * @param array $a
  200. * @param array $b
  201. * @return array
  202. */
  203. public static function merge(array $a, array $b)
  204. {
  205. foreach ($b as $key => $value) {
  206. if (array_key_exists($key, $a)) {
  207. if (is_int($key)) {
  208. $a[] = $value;
  209. } elseif (is_array($value) && is_array($a[$key])) {
  210. $a[$key] = self::merge($a[$key], $value);
  211. } else {
  212. $a[$key] = $value;
  213. }
  214. } else {
  215. $a[$key] = $value;
  216. }
  217. }
  218. return $a;
  219. }
  220. }