Req #76214 [Opn->Nab]: uasort different in different vesion
| From: | danack@php.net | Date: | Thu, 12 Apr 2018 12:18:37 +0000 |
| Subject: | Req #76214 [Opn->Nab]: uasort different in different vesion | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-214714@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76214&edit=1
ID: 76214
Updated by: danack@php.net
Reported by: gaozhenan at 126 dot com
Summary: uasort different in different vesion
-Status: Open
+Status: Not a bug
Type: Feature/Change Request
Package: *General Issues
PHP Version: 7.1.16
Block user comment: N
Private report: N
New Comment:
For arrays where items have the same value, you can't depend on them being sorted in a
repeatable format.
From the manual page http://php.net/manual/en/function.uasort.php.
Note:
If two members compare as equal, their relative order in the sorted array is undefined.
I'd suggest adding an element to the array before sorting, that stores what position the item
was at before the sorting, and then in the comparison function, if the scores are equal, also
compare by the initial position for the entries.
Previous Comments:
------------------------------------------------------------------------
[2018-04-12 11:55:23] gaozhenan at 126 dot com
Description:
------------
uasort's problem in different version(5.x, 7.x)
Test script:
---------------
<?php
$arr = array(
38 => ['id' => 38, 'score' => 3.45],
34 => ['id' => 34, 'score' => 3.45],
30 => ['id' => 30, 'score' => 3.45],
);
uasort($arr, function ($row_a, $row_b) {
if ($row_a['score'] == $row_b['score']) {
return 0;
}
return ($row_a['score'] >= $row_b['score']) ? -1 : 1;
});
print_r($arr);
~/test/uasort.php[1]
[gaozhenan@sjs_85_168 test]$php55 uasort.php
Array
(
[30] => Array
(
[id] => 30
[score] => 3.45
)
[34] => Array
(
[id] => 34
[score] => 3.45
)
[38] => Array
(
[id] => 38
[score] => 3.45
)
)
[gaozhenan@sjs_85_168 test]$php71 uasort.php
Array
(
[38] => Array
(
[id] => 38
[score] => 3.45
)
[34] => Array
(
[id] => 34
[score] => 3.45
)
[30] => Array
(
[id] => 30
[score] => 3.45
)
)
Expected result:
----------------
the result in php 7.x should be equal in 5.x
Actual result:
--------------
just opposite
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76214&edit=1