Cannot unpack array with string keys
The Cannot unpack array with string keys error is a classic JavaScript quirk that trips up developers when they try to use the rest operator (...) on arrays containing key-value pairs instead of raw values. It usually appears when you attempt to spread an object or an array of objects directly into a function call without first extracting the values.
In your specific scenario, the error stems from how PHP handles function argument unpacking versus how JavaScript handles object spreading. The rest operator (...) expects an iterable collection of arguments (like [1, 2, 3]) to pass as separate entities. When you pass ['q' => '...', 'bind' => [...]], the spread operator tries to iterate over the keys 'q' and 'bind'. Since these are strings, not values meant to be unpacked further into a function signature expecting specific positional or named parameters in that format, PHP throws this fatal error.
The Structural Mismatch
Looking at your Model::fetch() method:
public function fetch() {
return ['q' => (string)$this->sql,
'bind' => $this->vals ];
}
This returns a standard associative array (or a stdClass depending on how you instantiate it later). When you call ModelController::execute(...$q->fetch()), PHP attempts to unpack this array. It interprets the keys 'q' and 'bind' as literal argument names if the target function accepts named parameters, or it tries to treat them as values if they don't.
If ModelController::execute is defined as:
public static function execute($sql, $vals) { ... }
It expects two positional arguments. The rest operator will try to pass the first element of the unpacked array as the first argument and the second as the second. However, because the source array has explicit keys, PHP's internal logic regarding how it maps these to function parameters becomes ambiguous or throws an error depending on the specific PHP version and whether the target function uses named arguments (PHP 8.0+).
The Solution: Unpacking Values Only
You mentioned that running fetch() twice works. That is because you are bypassing the unpacking logic entirely, which confirms that the issue lies in how the data structure is being expanded.
To fix this while still using modern PHP features, you need to separate the retrieval of values from the unpacking operation. You have two primary options depending on your PHP version and preference for syntax.
Option 1: Extract Values Explicitly
The most robust approach that works across all versions is to extract the specific keys you want to pass. Since fetch() returns a simple array with known keys ('q' and 'bind'), you can access them directly before passing them to the function.
public function exec_all() {
foreach($this->queries as $q) {
$data = $q->fetch();
// Extract values by key, ensuring we don't unpack the associative structure itself
ModelController::execute(
$data['q'],
$data['bind']
);
}
}
This is verbose but safe. It clearly communicates that you are passing the value associated with 'q' and the value associated with 'bind'.
Option 2: Using ... with Specific Keys (PHP 8+)
If you are running PHP 8.0 or later, you can use named arguments with the rest operator to unpack specific keys. This allows you to keep some of the syntactic sugar you were aiming for while avoiding the ambiguity of unpacking the whole array.
public function exec_all() {
foreach($this->queries as $q) {
$data = $q->fetch();
// Unpack only specific keys, treating them as named arguments
ModelController::execute(
'q' => $data['q'],
'bind' => $data['bind']
);
}
}
Alternatively, if ModelController::execute is defined to accept these exact parameter names:
public static function execute(string $sql, array $vals) { ... }
And you want to use the spread operator on a subset of keys, PHP 8+ allows you to spread an associative array into named arguments if the function signature matches those keys exactly. However, since your fetch() returns an array with keys 'q' and 'bind', you must ensure the target function accepts them as such, or pass them as positional arguments by extracting them first.
Why This Happens
The core issue is that ...$array iterates over the array to create a list of arguments. If $array is [ 'q' => 'value1', 'bind' => 'value2' ], the unpacked list becomes 'value1', 'value2'. The function call effectively becomes execute('value1', 'value2').
The error occurs because PHP's internal mechanism for handling this unpacking with associative arrays can fail if the keys do not match expected named argument patterns or if the iteration logic conflicts with how the function expects to receive its parameters. By explicitly extracting $data['q'] and $data['bind'], you remove the ambiguity and ensure that exactly two values are passed to execute.
Final Recommendation
Unless you are strictly adhering to a PHP 8+ codebase where named argument spreading is your primary goal, sticking to explicit array key access ($data['key']) is often cleaner for data structures like this. It avoids the overhead of iterating over the array keys just to pass two values and makes the intent of the code immediately clear to other developers reading it.