Skip to content

Next index for array returned from array_keys() is wrong #23576

Description

@nox-4O4

Description

The following code:

<?php

$a = array(123 => 123);
unset($a[123]);

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

var_dump($b);

Resulted in this output:

array(1) {
  [124]=>
  int(42)
}

But I expected this output instead:

array(1) {
  [0]=>
  int(42)
}

The behavior changed during a patch version update: https://3v4l.org/KR2JQ#veol

This only happens when array_keys() is provided with an empty array that previously contained some elements.

PHP Version

PHP 8.4.25 (cli) (built: Aug 28 2026 07:36:03) (NTS)
Copyright (c) The PHP Group
Built by Debian
Zend Engine v4.4.25, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.25, Copyright (c), by Zend Technologies

Operating System

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions