Home > Backend Development > PHP Tutorial > Solution to the problem of usort changing the original position when the values ​​​​are the same in PHP_PHP tutorial

Solution to the problem of usort changing the original position when the values ​​​​are the same in PHP_PHP tutorial

WBOY
Release: 2016-07-21 15:22:18
Original
1059 people have browsed it

From PHP 4.1.0 onwards, the original position of usort may change when the compared values ​​are the same. The documentation says this:
If two members compare as equal, their order in the sorted array is undefined.
That is, if the 2 values ​​being compared are the same, their order in the sorted result is random. If you need to keep the original position of the same value, you can refer to the method in this article.
Demo data:

Copy code The code is as follows:

/*
Solve the problem of usort changing the original position when the values ​​​​are the same in PHP
Author: Artlover http://www.CodeBit.cn
*/
$arr = array(
array('a' = > 5, 'b' => 3),
array('a' => 5, 'b' => 1),
array('a' => 5, 'b ' => 4),
array('a' => 5, 'b' => 2),
);
?>

array The value of the first element in is the same. The expected result is to keep the existing position unchanged, that is, the order of b is 3, 1, 4, 2
Use usort to sort, when the values ​​of the compared fields are the same, The original order may change
Copy code The code is as follows:

/*
Solution The problem of usort changing the original position when the values ​​​​are the same in PHP
Author: Artlover http://www.CodeBit.cn
*/
$callback = create_function('$a,$b', 'return ($a["a"] == $b["a"])?0:(($a["a"] > $b["a"]) ? 1 : -1);');
usort($arr, $callback);
?>

Result:
Copy code Code As follows:

Array
(
[0] => Array
(
[a] => 5
[b] => 2
)
[1] => Array
(
[a] => 5
[b] => 4
)
[2] => ; Array
(
[a] => 5
[b] => 1
)
[3] => Array
(
[a] => 5
[b] => 3
)
)

Although the values ​​of the sort fields are the same, usort disrupts the order of the entire array.
If you want to keep the original position when the compared values ​​are the same, you can use array_multisort:
Copy the code The code is as follows:

/*
Solve the problem of usort changing the original position when the values ​​​​are the same in PHP
Author: Artlover http://www.CodeBit.cn
*/
/ / Index counter
$i = 0;
// Create 2 empty arrays, the first one holds the field to be sorted, and the second one holds the original index information
$a = $index = array() ;
foreach ($arr as $key => $data) {
$a[$key] = $data['a'];
$index[] = $i++;
}
// Sort the first array first, then sort by the original index
array_multisort($a, SORT_ASC, $index, SORT_ASC, $arr);
?>

Result:
Copy code The code is as follows:

Array
(
[0] => Array
(
[a] => 5
[b] => 3
)
[1] => Array
(
[a] = > 5
[b] => 1
)
[2] => Array
(
[a] => 5
[b] => 4
)
[3] => Array
(
[a] => 5
[b] => 2
)
)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324740.htmlTechArticleFrom PHP 4.1.0, the original position of usort may change when the compared values ​​are the same. The document is It says this: If two members compare as equal, their order in the sorted array is un...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template