arr.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * Return the first element in an array which passes a given truth test.
  68. *
  69. * <code>
  70. * // Return the first array element that equals "Taylor"
  71. * $value = Arr::first($array, function($k, $v) {return $v === 'Taylor';});
  72. *
  73. * // Return a default value if no matching element is found
  74. * $value = Arr::first($array, function($k, $v) {return $v === 'Taylor'}, 'Default');
  75. * </code>
  76. *
  77. * @param array $array
  78. * @param Closure $callback
  79. * @return mixed
  80. */
  81. public static function first($array, $callback, $default = null)
  82. {
  83. foreach ($array as $key => $value)
  84. {
  85. if (call_user_func($callback, $key, $value)) return $value;
  86. }
  87. return ($default instanceof Closure) ? call_user_func($default) : $default;
  88. }
  89. /**
  90. * Remove all array values that are contained within a given array of values.
  91. *
  92. * <code>
  93. * // Remove all array values that are empty strings
  94. * $array = Arr::without($array, '');
  95. *
  96. * // Remove all array values that are "One", "Two", or "Three"
  97. * $array = Arr::without($array, array('One', 'Two', 'Three'));
  98. * </code>
  99. *
  100. * @param array $array
  101. * @param array $without
  102. * @return array
  103. */
  104. public static function without($array, $without = array())
  105. {
  106. foreach ((array) $array as $key => $value)
  107. {
  108. if (in_array($value, $without)) unset($array[$key]);
  109. }
  110. return $array;
  111. }
  112. }