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

未关闭
#37 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
30/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
php
领域
backend

调研方向

Start by reading src/Support/Collection.php and locating dotGet(), whose implementation and missing return are shown in the issue. Check how the collection's get('b.b1') call reaches this method; done means the dotted lookup returns the nested value instead of null.

由索引模型根据 Issue 内容生成。

描述

版本: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;
    }
主要语言
PHP
星标
125
派生
17
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。