60

The error message is:

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?

It 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 content here')->bind(['example' => 'example value here']), 'one');
$m->exec_all();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • 1
    Do a `$values = array_values($q->fetch()); ModelController::execute(...$values)` – apokryfos Nov 17 '16 at 19:44
  • That completely worked! Could you provide an answer to what `array_values()` does in this situation in relation to unpacking? It would be appreciated @apokryfos – Jaquarh Nov 17 '16 at 19:46

3 Answers3

98

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. This will make calling a function using the array unpacking operator and an associative array work, as long as the array keys match the argument names. 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);
apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • 17
    This completely ruins the principle of using associative arrays, the key/index was going to be used as the SQL placeholder match so maybe its a limitation to what I need? Thanks a lot for the explanation! I'll mark when I can – Jaquarh Nov 17 '16 at 19:55
  • 2
    I'd expect it to be a limitation on how functions work with their arguments lists, e.g. `func_get_args()` is always numerically indexed. – apokryfos Nov 17 '16 at 19:57
  • Look's like it is back to the drawing board then haha, appreciated this knowledge however! – Jaquarh Nov 17 '16 at 19:58
  • 1
    If you are expecting to pass an associative array to a function in this manner, rewrite your method to take an array: `function execute(array $arguments) { var_dump($array['vals']); }` – Jay Bienvenu Nov 23 '18 at 15:33
  • Support for named arguments will come in PHP 8.1. It's not available in PHP 8.0. Source: https://www.amitmerchant.com/array-unpacking-with-string-keys-coming-in-php-81/ – Boschman Mar 11 '21 at 12:50
  • @Boschman that link talks about array unpacking with string keys. Named argument support has already hit 8.0 as seen [here](http://sandbox.onlinephpfunctions.com/code/973b885e14615d6cf1c8e4d371b4b93af20a9210) – apokryfos Mar 11 '21 at 13:18
  • Ah, sorry, then I misunderstood. Thanks for your reply! – Boschman Mar 11 '21 at 13:28
  • Well you could also use `...(array_values($params))` if for a function or just use `call_user_func_array` https://www.php.net/manual/fr/function.call-user-func-array.php – Tofandel Mar 16 '22 at 11:10
  • another interesting behaviour is, unpacking assocArray into a php8 function uses the keys as named parameters and will still break if the function parms do not match the array keys. – Brad Mar 20 '23 at 01:42
32

Update PHP 8.1

Since PHP 8.1, unpacking arrays also works with string-key arrays, so the error FATAL ERROR Uncaught Error: Cannot unpack array with string keys will not be applicable anymore.

Example:

$array1 = ["a" => 1];
$array2 = ["a" => 2];
$array = ["a" => 0, ...$array1, ...$array2];
var_dump($array); // ["a" => 2]

RFC: PHP RFC: Array unpacking with string keys

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jsowa
  • 9,104
  • 5
  • 56
  • 60
19

If you got this error when you tried to unpack an associative array into another array then you can use array_merge instead:

<?php

$inner = ["b" => "i", "c" => "i"];
$outer = ["a" => "o", "c" => "o"];

var_dump(array_merge($outer, $inner));
var_dump(array_merge($inner, $outer));

will produce

array(3) {
  ["a"]=>
  string(1) "o"
  ["c"]=>
  string(1) "i"
  ["b"]=>
  string(1) "i"
}

array(3) {
  ["b"]=>
  string(1) "i"
  ["c"]=>
  string(1) "o"
  ["a"]=>
  string(1) "o"
}
kaan_a
  • 3,503
  • 1
  • 28
  • 52