>Среда, 29 декабря 2021, 18:42 +03:00 от Vincent Langlet ><misterdevil...@gmail.com>: > >Hi, > >I recently discovered that an array was automatically casting >numeric-string keys to int if it was possible. For instance, the following >array: > >$a = ['01' => '01', '10' => '10']; > >Is not an array with the key '01' and '10' but instead consider the second >key as an int. > >This has some implications like the fact that >- array_flip(array_flip($a)) !== $a >- array_search('10', $a) is an int when array_search('01', $a) is still a >string. Someone using strict types and passing the result to a function >expecting a string could end with an unexpected crash. > >I've created an issue about this https://github.com/php/php-src/issues/7845 >but it was recommended to me to send a mail to this mailing list instead. > >I don't think this behavior should be considered as "normal" and would like >to propose to change this for PHP 9, as it's a BC-break. To me it can and >be considered as a follow-up of >https://wiki.php.net/rfc/string_to_number_comparison and >ttps://wiki.php.net/rfc/saner-numeric-strings >< https://wiki.php.net/rfc/saner-numeric-strings >. This is still a "concept" >since I never code with C and know nothing about the PHP implementation and >if this change would be possible. Any help is welcome then. > >Thanks I support this behavior fix because in its current form, due to a similar problem (almost?), all PSR-7 implementations contain bugs that violate RFC7230 (section 3.2: https://datatracker.ietf.org/doc/html/rfc7230#section-3.2 ). Thus, physically, by the standard, all headers can have the name "0" (like «0: value»), but when stored inside implementations, it is converted to a string and a problem arises ($message->getHeaders() // returns array<int|string, string> instead of array<string, string>).
To solve this problem without BC, it seems to me that Nikita's idea with «declares» is most suitable, like: ``` <?php declare(strict_types=1, strict_array_keys=1); $array["0"] = 42; // array(1) { string(1) "0" => int(42) } ``` -- Kirill Nesmeyanov