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

EW6.x 获取数组值的bug #2647

Closed
millsguo opened this issue Jan 17, 2023 · 0 comments
Closed

EW6.x 获取数组值的bug #2647

millsguo opened this issue Jan 17, 2023 · 0 comments

Comments

@millsguo
Copy link

millsguo commented Jan 17, 2023

我用的环境

  • PHP 版本:8.1.13
  • overtrue/wechat 版本:6.10
  • 是否使用了框架?框架名称:Zend Framework 1

问题及现象

直接抛异常
EasyWeChat\Kernel\Support\Arr::exists(): Argument #1 ($array) must be of type array, null given

在Arr的get方法中增加给$array赋值的判断,如果$array[$segment]值为空或不是数组,直接返回 $default,问题解决

    public static function get(array $array, string|int|null $key, mixed $default = null): mixed
    {
        if (is_null($key)) {
            return $array;
        }

        if (static::exists($array, $key)) {
            return $array[$key];
        }

        foreach (explode('.', (string) $key) as $segment) {
            /** @phpstan-ignore-next-line */
            if (static::exists($array, $segment)) {
                /** @phpstan-ignore-next-line */
                if (empty($array[$segment]) || !is_array($array[$segment])) {
                    return $default;
                }
                $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

2 participants