无法使用字符串键解压数组 [英] Cannot unpack array with string keys

查看:40
本文介绍了无法使用字符串键解压数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

致命错误未捕获错误:无法使用字符串键解压数组

FATAL ERROR Uncaught Error: Cannot unpack array with string keys

我知道我可以简单地运行方法 fetch() 两次并传递 ['q']['bind']但我正在尝试使用新的 ... 来解压缩值.我想传递这样的值:

I know I can simply run the method fetch() twice and pass the ['q'] and ['bind'] but I am trying to get to grips with using the new ... to unpack values. I want to pass the values like so:

(string) SQL, (Array) Bind Values

但我认为它试图从 fetch() 方法中解压绑定值数组和响应数组.是否可以解压这个数组(看起来像这样:)

But I think it tries to unpack the bind values array as well as the response array from the fetch() method. Is it possible to unpack this array (looks something like this:)

array(2) { 
    ["q"]=> string(7) "example" 
    ["bind"]=> array(1) {
        ["example"]=> string(3) "one"
    }
}

这是整个代码,以防万一您需要查看它们是如何组合在一起的:

This is the whole code, in-case you need to see how it all fits together:

class ModelController {
    public static function execute($sql, $vals) {
        var_dump($vals);
    }
}

class ModelContainer {

    private $queries = [];

    public function add_model(Model $model, $name) {
        $this->queries[$name] = $model;
        return $this;
    }

    public function exec_all() {
        foreach($this->queries as $q) {
            ModelController::execute(...$q->fetch());
        }
    }

    public function exec($name) {

    }

}

class Model {

    private $sql;
    private $vals = [];


    public function set_q($statement) {
        $this->sql = $statement;
        return $this;
    }

    public function bind($vals = []) {
        $this->vals = $vals;
        return $this;
    }

    public function fetch() {
        return ['q' => (string)$this->sql,
            'bind' => $this->vals ];
    }
}

$m = new ModelContainer();
$m->add_model((new Model)->set_q('example sql here')->bind(['example' => 'example value here']), 'one');
$m->exec_all();

推荐答案

问题在于 splat 运算符(数组解包运算符或 ...)不适用于关联数组.示例:

The problem is that the splat operator (array unpacking operator or ...) does not work with associative arrays. Example:

$array = [1, 2, 3];
$assoc = ["one" => 1, "two" => 2, "three" => 3];

var_dump(...$array); //Works
var_dump(...$assoc); //Doesn't work

您需要强制对数组进行数字索引以对其进行解包.您可以使用 array_values:

You need to force an array to be numerically indexed in order to unpack it. You do this using array_values:

$values = array_values($q->fetch());
ModelController::execute(...$values);

数组值将确保所有值都有一个连续的数字键.

Array values will ensure all values have a sequential numeric key.

更新

从 PHP 8 开始,将支持命名参数,这将使两种情况都能正常工作.提议的命名参数的 RFC 中记录了一个示例,它表示以下代码应该可以开始工作PHP 8

Starting PHP 8 there will be support for named arguments which will make both cases work. An example is documented in the proposed RFC for named arguments which says the following code should work starting PHP 8

$params = ['start_index' => 0, 'num' => 100, 'value' => 50];
array_fill(...$params);

这篇关于无法使用字符串键解压数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆