arr.php 3 KB
Newer Older
1
<?php namespace Laravel; use Closure;
2

3 4 5 6 7
class Arr {

	/**
	 * Get an item from an array.
	 *
8 9 10 11 12 13 14 15 16 17
	 * "Dot" notation may be used to dig deep into the array.
	 *
	 * <code>
	 *		// Get the $array['user']['name'] value from the array
	 *		$name = Arr::get($array, 'user.name');
	 *
	 *		// Return a default from if the specified item doesn't exist
	 *		$name = Arr::get($array, 'user.name', 'Taylor');
	 * </code>
	 *
18
	 * @param  array   $array
19
	 * @param  string  $key
20
	 * @param  mixed   $default
21 22
	 * @return mixed
	 */
23
	public static function get($array, $key, $default = null)
24
	{
Taylor Otwell committed
25
		if (is_null($key)) return $array;
26

Taylor Otwell committed
27 28
		foreach (explode('.', $key) as $segment)
		{
29
			if ( ! is_array($array) or ! array_key_exists($segment, $array))
Taylor Otwell committed
30
			{
31
				return ($default instanceof Closure) ? call_user_func($default) : $default;
Taylor Otwell committed
32 33 34 35 36 37 38 39
			}

			$array = $array[$segment];
		}

		return $array;
	}

Taylor Otwell committed
40
	/**
Taylor Otwell committed
41
	 * Set an array item to a given value.
Taylor Otwell committed
42
	 *
Taylor Otwell committed
43
	 * The same "dot" syntax used by the "get" method may be used here.
Taylor Otwell committed
44
	 *
45 46 47 48 49 50 51
	 * If no key is given to the method, the entire array will be replaced.
	 *
	 * <code>
	 *		// Set the $array['user']['name'] value on the array
	 *		Arr::set($array, 'user.name', 'Taylor');
	 * </code>
	 *
Taylor Otwell committed
52 53 54 55 56 57 58
	 * @param  array   $array
	 * @param  string  $key
	 * @param  mixed   $value
	 * @return void
	 */
	public static function set(&$array, $key, $value)
	{
59 60
		if (is_null($key)) return $array = $value;

61
		$keys = explode('.', $key);
Taylor Otwell committed
62

63
		while (count($keys) > 1)
Taylor Otwell committed
64
		{
65 66
			$key = array_shift($keys);

67
			if ( ! isset($array[$key]) or ! is_array($array[$key]))
Taylor Otwell committed
68
			{
69
				$array[$key] = array();
Taylor Otwell committed
70 71
			}

72
			$array =& $array[$key];
Taylor Otwell committed
73 74
		}

75
		$array[array_shift($keys)] = $value;
Taylor Otwell committed
76 77
	}

78 79 80
	/**
	 * Return the first element in an array which passes a given truth test.
	 *
81 82 83 84 85 86 87 88
	 * <code>
	 *		// Return the first array element that equals "Taylor"
	 *		$value = Arr::first($array, function($k, $v) {return $v === 'Taylor';});
	 *
	 *		// Return a default value if no matching element is found
	 *		$value = Arr::first($array, function($k, $v) {return $v === 'Taylor'}, 'Default');
	 * </code>
	 *
89 90 91 92
	 * @param  array    $array
	 * @param  Closure  $callback
	 * @return mixed
	 */
93
	public static function first($array, $callback, $default = null)
94 95 96 97 98
	{
		foreach ($array as $key => $value)
		{
			if (call_user_func($callback, $key, $value)) return $value;
		}
99

100
		return ($default instanceof Closure) ? call_user_func($default) : $default;
101 102
	}

Taylor Otwell committed
103
	/**
Taylor Otwell committed
104
	 * Remove all array values that are contained within a given array of values.
Taylor Otwell committed
105
	 *
106 107 108 109 110 111 112 113
	 * <code>
	 *		// Remove all array values that are empty strings
	 *		$array = Arr::without($array, '');
	 *
	 *		// Remove all array values that are "One", "Two", or "Three"
	 *		$array = Arr::without($array, array('One', 'Two', 'Three'));
	 * </code>
	 *
Taylor Otwell committed
114 115 116 117 118 119
	 * @param  array  $array
	 * @param  array  $without
	 * @return array
	 */
	public static function without($array, $without = array())
	{
120 121
		$without = (array) $without;

122
		foreach ((array) $array as $key => $value)
Taylor Otwell committed
123 124 125 126 127 128 129
		{
			if (in_array($value, $without)) unset($array[$key]);
		}

		return $array;
	}

130
}