arr.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php namespace Laravel; use Closure;
  2. class Arr {
  3. /**
  4. * Get an item from an array.
  5. *
  6. * "Dot" notation may be used to dig deep into the array.
  7. *
  8. * <code>
  9. * // Get the $array['user']['name'] value from the array
  10. * $name = Arr::get($array, 'user.name');
  11. *
  12. * // Return a default from if the specified item doesn't exist
  13. * $name = Arr::get($array, 'user.name', 'Taylor');
  14. * </code>
  15. *
  16. * @param array $array
  17. * @param string $key
  18. * @param mixed $default
  19. * @return mixed
  20. */
  21. public static function get($array, $key, $default = null)
  22. {
  23. if (is_null($key)) return $array;
  24. foreach (explode('.', $key) as $segment)
  25. {
  26. if ( ! is_array($array) or ! array_key_exists($segment, $array))
  27. {
  28. return ($default instanceof Closure) ? call_user_func($default) : $default;
  29. }
  30. $array = $array[$segment];
  31. }
  32. return $array;
  33. }
  34. /**
  35. * Set an array item to a given value.
  36. *
  37. * The same "dot" syntax used by the "get" method may be used here.
  38. *
  39. * If no key is given to the method, the entire array will be replaced.
  40. *
  41. * <code>
  42. * // Set the $array['user']['name'] value on the array
  43. * Arr::set($array, 'user.name', 'Taylor');
  44. * </code>
  45. *
  46. * @param array $array
  47. * @param string $key
  48. * @param mixed $value
  49. * @return void
  50. */
  51. public static function set(&$array, $key, $value)
  52. {
  53. if (is_null($key)) return $array = $value;
  54. $keys = explode('.', $key);
  55. while (count($keys) > 1)
  56. {
  57. $key = array_shift($keys);
  58. if ( ! isset($array[$key]) or ! is_array($array[$key]))
  59. {
  60. $array[$key] = array();
  61. }
  62. $array =& $array[$key];
  63. }
  64. $array[array_shift($keys)] = $value;
  65. }
  66. /**
  67. * Remove an array item from a given array.
  68. *
  69. * The same "dot" syntax used by the "get" method may be used here.
  70. *
  71. * <code>
  72. * // Remove the $array['user']['name'] item from the array
  73. * Arr::forget($array, 'user.name');
  74. * </code>
  75. *
  76. * @param array $array
  77. * @param string $key
  78. * @param mixed $value
  79. * @return void
  80. */
  81. public static function forget(&$array, $key)
  82. {
  83. if (is_null($key)) return;
  84. $keys = explode('.', $key);
  85. while (count($keys) > 1)
  86. {
  87. $key = array_shift($keys);
  88. if ( ! isset($array[$key]) or ! is_array($array[$key]))
  89. {
  90. return;
  91. }
  92. $array =& $array[$key];
  93. }
  94. unset($array[array_shift($keys)]);
  95. }
  96. /**
  97. * Return the first element in an array which passes a given truth test.
  98. *
  99. * <code>
  100. * // Return the first array element that equals "Taylor"
  101. * $value = Arr::first($array, function($k, $v) {return $v === 'Taylor';});
  102. *
  103. * // Return a default value if no matching element is found
  104. * $value = Arr::first($array, function($k, $v) {return $v === 'Taylor'}, 'Default');
  105. * </code>
  106. *
  107. * @param array $array
  108. * @param Closure $callback
  109. * @return mixed
  110. */
  111. public static function first($array, $callback, $default = null)
  112. {
  113. foreach ($array as $key => $value)
  114. {
  115. if (call_user_func($callback, $key, $value)) return $value;
  116. }
  117. return ($default instanceof Closure) ? call_user_func($default) : $default;
  118. }
  119. /**
  120. * Remove all array values that are contained within a given array of values.
  121. *
  122. * <code>
  123. * // Remove all array values that are empty strings
  124. * $array = Arr::without($array, '');
  125. *
  126. * // Remove all array values that are "One", "Two", or "Three"
  127. * $array = Arr::without($array, array('One', 'Two', 'Three'));
  128. * </code>
  129. *
  130. * @param array $array
  131. * @param array $without
  132. * @return array
  133. */
  134. public static function without($array, $without = array())
  135. {
  136. $without = (array) $without;
  137. foreach ((array) $array as $key => $value)
  138. {
  139. if (in_array($value, $without)) unset($array[$key]);
  140. }
  141. return $array;
  142. }
  143. }