improve arr_set()

This commit is contained in:
xiaomlove
2021-01-14 02:27:55 +08:00
parent 0541f2a6c0
commit f4d887531c

View File

@@ -233,7 +233,7 @@ function config($key, $default = null)
* @param null $name * @param null $name
* @return array|mixed|string * @return array|mixed|string
*/ */
function get_setting($name = null) function get_setting($name = null, $prefix = null)
{ {
static $settings; static $settings;
if (is_null($settings)) { if (is_null($settings)) {
@@ -342,15 +342,21 @@ function arr_get($array, $key, $default = null)
function arr_set(&$array, $key, $value) function arr_set(&$array, $key, $value)
{ {
if (strpos($key, '.') === false) { $parts = explode('.', $key);
$array[$key] = $value; $last = null;
} while (true) {
foreach (explode('.', $key) as $segment) { $segment = array_pop($parts);
if (isset($array[$segment])) { if (empty($segment)) {
$array = $array[$segment]; return $array;
} else { }
return $default; if (is_null($last)) {
} $array[$segment] = $value;
} } else {
return $array; $array[$segment] = $array;
unset($array[$last]);
}
$last = $segment;
}
return $array;
} }