http icon indicating copy to clipboard operation
http copied to clipboard

Overtrue\Http\Support\Collection::dotGet()方法无法正确获取“.”号分割的key

Open wangjikill opened this issue 3 years ago • 0 comments

版本:1.2.3 文件:src/Support/Collection.php PHP版本:8.1 问题描述: 一个形如:

$config = new Collection([
    'a' => 'a0',
    'b' => [
            'b1' => 'b10'
           ]
]);

var_dump($config->get('b.b1')); //result is "null"

原因:

    /**
     * @param array      $array
     * @param string     $key
     * @param mixed|null $default
     *
     * @return mixed
     */
    public function dotGet($array, $key, $default = null)
    {
        if (is_null($key)) {
            return $array;
        }
        if (array_key_exists($key, $array)) {
            return $array[$key];
        }
        foreach (explode('.', $key) as $segment) {
            if (array_key_exists($segment, $array)) {
                $array = $array[$segment];
            } else {
                return $default;
            }
        }

        // 缺少循环完的返回 return $array;
    }

wangjikill avatar Dec 29 '22 04:12 wangjikill