diff --git a/system/arr.php b/system/arr.php index 0b65a831..f05eeeef 100644 --- a/system/arr.php +++ b/system/arr.php @@ -31,4 +31,36 @@ public static function get($array, $key, $default = null) return $array; } + /** + * Set an item in an array. + * + * This method is primarly helpful for setting the value in an array with + * a variable depth, such as configuration files. + * + * Like the Arr::get method, JavaScript "dot" syntax is supported. + * + * @param array $array + * @param string $key + * @param mixed $value + * @return void + */ + public static function set(&$array, $key, $value) + { + $reference =& $array; + + foreach (explode('.', $key) as $segment) + { + if ( ! isset($reference[$segment])) + { + $reference[$segment] = $value; + + return; + } + + $reference =& $reference[$segment]; + } + + $reference = $value; + } + } \ No newline at end of file