Skip to content

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

Description

@x-tiantian

版本: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;
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions