Skip to content

ksort(..., SORT_REGULAR) behaves incorrectly on arrays with string and numeric keys since PHP 8.0 #9296

Closed
@astrophysik

Description

@astrophysik

Description

The behavior of the string and number comparison operators has been changed since php 8, but ksort and krsort functions still use the old rules.
It leads to inconsistent situations, when after ksort the first key is greater than some of the next ones by according to the basic comparison operators.

The following code:

<?php
$array = ["600" => 0, "aaa" => 1];
ksort($array, SORT_REGULAR);
var_dump($array);
var_dump(array_key_first($array) <=> array_key_last($array));

Resulted in this output:

array(2) {
  ["aaa"]=>
  int(1)
  [600]=>
  int(0)
}
int(1)

But I expected this output instead:

array(2) {
  [600]=>
  int(0)
  ["aaa"]=>
  int(1)
}
int(-1)

PHP Version

PHP 8.0

Operating System

No response

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