added arr::set method.

This commit is contained in:
Taylor Otwell 2011-08-15 00:16:37 -05:00
parent 39d323c44f
commit a667e32ad3
1 changed files with 32 additions and 0 deletions

View File

@ -31,4 +31,36 @@ public static function get($array, $key, $default = null)
return $array; 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;
}
} }