Fix GH-23576: array_keys() on an empty array returns a non-zero next index - #23577
Open
lazerg wants to merge 1 commit into
Open
Fix GH-23576: array_keys() on an empty array returns a non-zero next index#23577lazerg wants to merge 1 commit into
lazerg wants to merge 1 commit into
Conversation
devnexen
reviewed
Sep 5, 2026
| GH-23576 (Next index for array returned from array_keys() is wrong) | ||
| --FILE-- | ||
| <?php | ||
| $a = [123 => 123]; |
Member
There was a problem hiding this comment.
would be nice to also test an empty array ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
array_keys()returns the input array itself when that array is empty. An array that has held elements keeps its next free index, soarray_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: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 whatarray_values()right below already does for the same case.Fixes GH-23576