mirror of
https://github.com/php/php-src.git
synced 2026-03-26 01:02:25 +01:00
Make user-exposed sorts stable, by storing the position of elements in the original array, and using those positions as a fallback comparison criterion. The base sort is still hybrid q/insert. The use of true/false comparison functions is deprecated (but still supported) and should be replaced by -1/0/1 comparison functions, driven by the <=> operator. RFC: https://wiki.php.net/rfc/stable_sorting Closes GH-5236.
39 lines
626 B
PHP
39 lines
626 B
PHP
--TEST--
|
|
Bug #71334: Cannot access array keys while uksort()
|
|
--FILE--
|
|
<?php
|
|
|
|
class myClass
|
|
{
|
|
private $a = [
|
|
'foo-test' => [1],
|
|
'-' => [2],
|
|
'bar-test' => [3]
|
|
];
|
|
|
|
private function _mySort($x, $y)
|
|
{
|
|
if (!isset($this->a[$x])) {
|
|
throw new Exception('Missing X: "' . $x . '"');
|
|
}
|
|
|
|
if (!isset($this->a[$y])) {
|
|
throw new Exception('Missing Y: "' . $y . '"');
|
|
}
|
|
|
|
return $x <=> $y;
|
|
}
|
|
|
|
public function __construct()
|
|
{
|
|
uksort($this->a, [$this, '_mySort']);
|
|
}
|
|
}
|
|
|
|
new myClass();
|
|
echo "Done";
|
|
|
|
?>
|
|
--EXPECT--
|
|
Done
|