No Description
unmaintained
experiment
php

arr.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * PHPLib
  4. *
  5. * A PHP library to wrap php functions in a standard fashion
  6. *
  7. * @package PHPLib
  8. * @author Timothy J. Warren
  9. * @copyright Copyright (c) 2013
  10. * @link https://github.com/aviat4ion/PHPLib
  11. * @license MIT
  12. */
  13. // --------------------------------------------------------------------------
  14. namespace PHPUtil;
  15. /**
  16. * Wrapper over array functions with a consistent interface
  17. */
  18. class Arr {
  19. /**
  20. * Original array to manipulate
  21. * @var array
  22. */
  23. protected $original;
  24. /**
  25. * Constructor
  26. *
  27. * @param array $arr
  28. * @return Arr
  29. */
  30. public function __construct($arr)
  31. {
  32. $this->original = $arr;
  33. }
  34. /**
  35. * Enable using the object as a function
  36. *
  37. * @param string $arr
  38. * @return $this
  39. */
  40. public function __invoke($arr)
  41. {
  42. $this->__construct($arr);
  43. return $this;
  44. }
  45. /**
  46. * Return the keys or a subset of the keys of an array
  47. *
  48. * @see http://php.net/manual/function.array-keys.php
  49. * @param mixed $search_value
  50. * @param bool $strict
  51. * @return array
  52. */
  53. public function keys($search_value=NULL, $strict=FALSE)
  54. {
  55. return array_keys($this->original, $search_value, $strict);
  56. }
  57. /**
  58. * Returns the values from the input array with a numeric index
  59. *
  60. * @return array
  61. */
  62. public function values()
  63. {
  64. return array_values($this->original);
  65. }
  66. /**
  67. * Returns an array of elements from the input array that match the
  68. * specified pattern
  69. *
  70. * @param mixed $pattern
  71. * @param mixed $invert
  72. * @return array
  73. */
  74. public function grep($pattern, $invert=FALSE)
  75. {
  76. $flags = ($invert == TRUE) ? PREG_GREP_INVERT : 0;
  77. return preg_grep($pattern, $this->original, $flags);
  78. }
  79. /**
  80. * Replace elements in the main array with the values in the passed arrays
  81. * (Takes a variable number of arguments)
  82. *
  83. * @see http://php.net/manual/function.array-replace.php
  84. * @return array
  85. */
  86. public function replace()
  87. {
  88. $args = func_get_args();
  89. array_unshift($args, $this->original);
  90. return call_user_func_array('array_replace', $args);
  91. }
  92. /**
  93. * Combine arrays
  94. * (Takes a variable number of arguments)
  95. *
  96. * @see http://php.net/manual/function.array-merge.php
  97. * @return array
  98. */
  99. public function merge()
  100. {
  101. $args = func_get_args();
  102. array_unshift($args, $this->original);
  103. return call_user_func_array('array_merge', $args);
  104. }
  105. /**
  106. * Return a new array from the original with duplicate items removed
  107. *
  108. * @see http://php.net/manual/function.array-unique.php
  109. * @param
  110. * @return array
  111. */
  112. public function unique($sort_flags=SORT_STRING)
  113. {
  114. return array_unique($this->original, $sort_flags);
  115. }
  116. /**
  117. * Return an array with elements having the defined values
  118. *
  119. * @see http://php.net/manual/function.array-fill.php
  120. * @param int $start_index
  121. * @param int $num
  122. * @param mixed $value
  123. * @return array
  124. */
  125. public function fill($start_index, $num, $value)
  126. {
  127. return array_file($start_index, $num, $value);
  128. }
  129. /**
  130. * Lengthen array to $pad_length with $pad_value
  131. *
  132. * @see http://php.net/manual/function.array-pad.php
  133. * @param int $pad_size
  134. * @param mixed $pad_value
  135. * @return array
  136. */
  137. public function pad($pad_size, $pad_value)
  138. {
  139. return array_pad($this->original, $pad_size, $pad_value);
  140. }
  141. /**
  142. * Return an array with keys that are the values of the input
  143. * array, and values as the frequency of those values
  144. *
  145. * @see http://php.net/manual/function.array-count-values.php
  146. * @return array
  147. */
  148. public function count_values()
  149. {
  150. return array_count_values($this->original);
  151. }
  152. /**
  153. * Return an array with keys and values reversed from the input array
  154. *
  155. * @see http://php.net/manual/function.array-flip.php
  156. * @return array
  157. */
  158. public function flip()
  159. {
  160. return array_flip($this->original);
  161. }
  162. /**
  163. * Returns the sum of all the values in the array
  164. *
  165. * @see http://php.net/manual/function.array-sum.php
  166. * @return number
  167. */
  168. public function sum()
  169. {
  170. return array_sum($this->original);
  171. }
  172. /**
  173. * Return a sorted array
  174. *
  175. * @see http://php.net/manual/function.asort.php
  176. * @param int $sort_flags
  177. * @return array
  178. */
  179. public function sort($sort_flags=SORT_REGULAR)
  180. {
  181. asort($this->original, $sort_flags);
  182. return $this->original;
  183. }
  184. /**
  185. * Return a reverse-sorted array
  186. *
  187. * @see http://php.net/manual/function.asort.php
  188. * @param int $sort_flags
  189. * @return array
  190. */
  191. public function reverse_sort($sort_flags=SORT_REGULAR)
  192. {
  193. rsort($this->original, $sort_flags);
  194. return $this->original;
  195. }
  196. /**
  197. * Reverse the order of the array
  198. *
  199. * @see http://php.net/manual/function.array-reverse.php
  200. * @param bool $preserve_keys
  201. * @return array
  202. */
  203. public function reverse($preserve_keys=FALSE)
  204. {
  205. return array_reverse($this->original, $preserve_keys);
  206. }
  207. /**
  208. * Filter elements of the input array using a callback function
  209. *
  210. * @see http://php.net/manual/function.array-filter.php
  211. * @param callable $callback
  212. * @return array
  213. */
  214. public function filter(callable $callback=NULL)
  215. {
  216. return array_filter($this->original, $callback);
  217. }
  218. }
  219. // End of arr.php