Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ PHP NEWS
. Fixed a memory leak in array_merge_recursive() when the recursive merge of
an object converted to an array fails. (David Carlier)
. Fixed read buffer compaction in php_stream_filter_flush(). (crystarm)
. Fixed bug GH-23576 (Next index for array returned from array_keys() is
wrong). (Lazizbek Ergashev)

- SimpleXML:
. Fixed writing to a dimension of the object returned by attributes() not
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4504,7 +4504,7 @@ PHP_FUNCTION(array_keys)

/* Base case: empty input */
if (!elem_count) {
RETURN_COPY(input);
RETURN_EMPTY_ARRAY();
}

/* Initialize return array */
Expand Down
58 changes: 58 additions & 0 deletions ext/standard/tests/array/gh23576.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--TEST--
GH-23576 (Next index for array returned from array_keys() is wrong)
--FILE--
<?php
$a = [123 => 123];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to also test an empty array ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, thanks. Added in 457a21e. The test now also runs all three array_keys() forms on an empty array.

unset($a[123]);

$b = array_keys($a);
$b[] = 42;
var_dump($b);

$c = array_keys($a, 123);
$c[] = 42;
var_dump($c);

$d = array_keys($a, 123, true);
$d[] = 42;
var_dump($d);

$e = [];

$f = array_keys($e);
$f[] = 42;
var_dump($f);

$g = array_keys($e, 123);
$g[] = 42;
var_dump($g);

$h = array_keys($e, 123, true);
$h[] = 42;
var_dump($h);
?>
--EXPECT--
array(1) {
[0]=>
int(42)
}
array(1) {
[0]=>
int(42)
}
array(1) {
[0]=>
int(42)
}
array(1) {
[0]=>
int(42)
}
array(1) {
[0]=>
int(42)
}
array(1) {
[0]=>
int(42)
}