Bug #69158 [Opn->Nab]: Inconsistency: usort does not return the same result on PHP < 7 and PHP7
Edit report at https://bugs.php.net/bug.php?id=69158&edit=1
ID: 69158
Updated by: aharvey@php.net
Reported by: contact at jubianchi dot fr
Summary: Inconsistency: usort does not return the same result
on PHP < 7 and PHP7
-Status: Open
+Status: Not a bug
Type: Bug
Package: Arrays related
PHP Version: master-Git-2015-03-02 (Git)
Block user comment: N
Private report: N
New Comment:
This is expected: PHP's sorting functions aren't guaranteed to be stable ($a and $b are
"equal" in this case, since the array callback will return 0 as their file and line
elements are identical), and there have been implementation changes in PHP 7 that affect this case.
Previous Comments:
------------------------------------------------------------------------
[2015-03-02 14:49:00] contact at jubianchi dot fr
Description:
------------
There is a tiny difference on usort result between php < 7 and php7 (master): items are not
ordered the same way between those versions.
PHP7 seems to be consistent with HVVM but not with earlier version of PHP :)
Test script:
---------------
<?php
// http://3v4l.org/RYTt9
$array = array(
array(
'file' => 'file1',
'line' => 1,
'message' => 'foo'
),
array(
'file' => 'file1',
'line' => 1,
'message' => 'bar'
)
);
usort($array, function($a, $b) {
if ($a['file'] !== $b['file'])
{
return strcmp($a['file'], $b['file']);
}
else if ($a['line'] === $b['line'])
{
return 0;
}
else
{
return ($a['line'] < $b['line'] ? -1 : 1);
}
}
);
var_dump($array);
Expected result:
----------------
array(2) {
[0]=>
array(3) {
["file"]=>
string(5) "file1"
["line"]=>
int(1)
["message"]=>
string(3) "bar"
}
[1]=>
array(3) {
["file"]=>
string(5) "file1"
["line"]=>
int(1)
["message"]=>
string(3) "foo"
}
}
Actual result:
--------------
array(2) {
[0]=>
array(3) {
["file"]=>
string(5) "file1"
["line"]=>
int(1)
["message"]=>
string(3) "foo"
}
[1]=>
array(3) {
["file"]=>
string(5) "file1"
["line"]=>
int(1)
["message"]=>
string(3) "bar"
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69158&edit=1
Thread (6 messages)