[php-src] Issue #7845: Stop to automatically cast numeric-string to int when using them as array-key
| From: | VincentLanglet | Date: | Tue, 28 Dec 2021 17:16:50 +0000 |
| Subject: | [php-src] Issue #7845: Stop to automatically cast numeric-string to int when using them as array-key | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-238607@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7845
Author: VincentLanglet
### Description
Currently we have
```
$a = ['01' => '01', '10' => '10'];
var_dump($a);
// (string) 01 => (string) 01
// (int) 10 => (string) 10
var_dump(array_flip($a));
// (string) 01 => (string) 01
// (int) 10 => (int) 10
var_dump(array_flip(array_flip($a)));
// (string) 01 => (string) 01
// (int) 10 => (int) 10
var_dump($a === array_flip(array_flip($a))); // false
```
Is there a reason to do this automatic cast from
'10' to 10 when
it's an array-key or wouldn't it be better to change this behavior for PHP 9 ?
To me it causes unexpected result like this one:
```
<?php
declare(strict_types=1);
function foo(string $string) {}
$array = ['01' => 5, '10' => 6];
$key = array_search(5, $array);
foo($key); // works
$key = array_search(6, $array);
foo($key); // crash
```