From dfbd8e7950d33458ef56c09d41f20564f584b9c4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 2 Aug 2011 13:38:50 -0500 Subject: [PATCH] Added Arr::dot method. --- system/arr.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/system/arr.php b/system/arr.php index ba3875e5..622f75dd 100644 --- a/system/arr.php +++ b/system/arr.php @@ -27,4 +27,27 @@ class Arr { 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; + } + } \ No newline at end of file