Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

FATAL ERROR Uncaught Error: Cannot unpack array with string keys

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

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
666 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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.

Update

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...