Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wangjikill opened this issue Dec 29, 2022 · 0 comments

Comments

@wangjikill
Copy link

版本: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;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant