[php-src] PR #23577: Fix GH-23576: array_keys() on an empty array returns a non-zero next index
| From: | lazerg | Date: | Fri, 04 Sep 2026 23:38:44 +0000 |
| Subject: | [php-src] PR #23577: Fix GH-23576: array_keys() on an empty array returns a non-zero next index | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-38313@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/23577
Author: lazerg
array_keys() returns the input array itself when that array is empty. An array that has
held elements keeps its next free index, so array_keys() on an
empty-but-previously-populated array hands back a next index that is not 0, and the first append
lands on the old index instead:
```php
$a = [123 => 123];
unset($a[123]);
$b = array_keys($a);
$b[] = 42; // ends up at key 124, not 0
```
The shortcut has been there since 7.2, but it only became visible in 7.3.17 / 7.4.5, when the fix
for bug #79364 made copying an empty array preserve the next free index. array_keys()
is documented to return a list, so returning the caller's array is wrong regardless of what the
copy preserves. Return a fresh empty array instead, which is what array_values() right
below already does for the same case.
Fixes GH-23576