From f4d887531c5cccd88ff9fc11c300d0f76b633102 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Thu, 14 Jan 2021 02:27:55 +0800 Subject: [PATCH] improve arr_set() --- include/globalfunctions.php | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/include/globalfunctions.php b/include/globalfunctions.php index 7e643928..16c7f01f 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -233,7 +233,7 @@ function config($key, $default = null) * @param null $name * @return array|mixed|string */ -function get_setting($name = null) +function get_setting($name = null, $prefix = null) { static $settings; if (is_null($settings)) { @@ -342,15 +342,21 @@ function arr_get($array, $key, $default = null) function arr_set(&$array, $key, $value) { - if (strpos($key, '.') === false) { - $array[$key] = $value; - } - foreach (explode('.', $key) as $segment) { - if (isset($array[$segment])) { - $array = $array[$segment]; - } else { - return $default; - } - } - return $array; + $parts = explode('.', $key); + $last = null; + while (true) { + $segment = array_pop($parts); + if (empty($segment)) { + return $array; + } + if (is_null($last)) { + $array[$segment] = $value; + } else { + $array[$segment] = $array; + unset($array[$last]); + } + $last = $segment; + + } + return $array; }