Added Arr::dot method.

This commit is contained in:
Taylor Otwell 2011-08-02 13:38:50 -05:00
parent 6c149b282f
commit dfbd8e7950
1 changed files with 23 additions and 0 deletions

View File

@ -27,4 +27,27 @@ public static function get($array, $key, $default = null)
return is_callable($default) ? call_user_func($default) : $default;
}
/**
* Get an item from an array using JavaScript style "dot" notation.
*
* @param array $array
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function dot($array, $key, $default = null)
{
foreach (explode('.', $key) as $segment)
{
if ( ! isset($array[$segment]))
{
return is_callable($default) ? call_user_func($default) : $default;
}
$array = $array[$segment];
}
return $array;
}
}