|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-07-12 02:09 UTC] nowm at yandex dot ru
Description: ------------ This request is related to any PHP 5.6+ When you pass an associative array for argument unpacking (https://wiki.php.net/rfc/argument_unpacking), you get a fatal error, "Cannot unpack array with string keys". As I see, there are no reasons to limit unpacking to only numeric arrays. For example, it does not use numeric keys for ordering values, so key type has no any sense: <?php $array = [1 => 'b', 2 => 'c', 0 => 'a']; // Outputs "b c a", not "a b c" // It shows that array unpacking does not rely on keys at all var_dump(...$array); Test script: --------------- <?php $array = ['a' => 'one', 'b' => 'two', 'c' => 'three']; var_dump(...$array); Expected result: ---------------- string(3) "one" string(3) "two" string(5) "three" Actual result: -------------- PHP Fatal error: Uncaught Error: Cannot unpack array with string keys in Standard input code:5 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 09:00:02 2025 UTC |
> so that if/when named arguments are supported, unpacking can be supported without breaking backwards compatibility But backward compatibility already will be broken in any case, because, for example, the "call_user_func_array" function allows associative arrays. So when (or if) named arguments are supported, a lot of code will be broken. I saw a lot of code that using associative arrays in the "call_user_func_array". <?php // It works! call_user_func_array('var_dump', ['a' => 1, 'b' => 'two']);