从 PHP 4.1.0 后,usort 在比较的值相同时,原始位置可能会改变,如果比较的2个值相同,则它们在排序结果中的顺序是随机的。如果你需要保持相同值的原始位置,可以参考本文的方法。
从 PHP 4.1.0 后,usort 在比较的值相同时,原始位置可能会改变,文档中是这样说的:
If two members compare as equal, their order in the sorted array is undefined.
也就是说,如果比较的2个值相同,则它们在排序结果中的顺序是随机的。如果你需要保持相同值的原始位置,可以参考本文的方法。
演示数据:
代码如下:
/*
解决 PHP 中 usort 在值相同时改变原始位置的问题
作者: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),
);
?>
数组中第一个元素的值是相同的,期望的结果是保持现有的位置不变,即 b 的顺序为 3,1,4,2
用 usort 排序,当比较字段的值相同时,原始顺序可能会改变
代码如下:
/*
解决 PHP 中 usort 在值相同时改变原始位置的问题
作者: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);
?>
结果:
代码如下:
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
)
)
虽然排序字段的值相同,但是 usort 却将整个数组的顺序打乱了。
如果要在比较的值相同时保持原始位置,可以用 array_multisort :
代码如下:
/*
解决 PHP 中 usort 在值相同时改变原始位置的问题
作者:Artlover http://www.CodeBit.cn
*/
// 索引计数器
$i = 0;
// 创建2个空数组,第一个保存要排序的字段,第二个保存原始索引信息
$a = $index = array();
foreach ($arr as $key => $data) {
$a[$key] = $data['a'];
$index[] = $i++;
}
// 第一个数组先排,接着按原始索引排
array_multisort($a, SORT_ASC, $index, SORT_ASC, $arr);
?>
结果:
代码如下:
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
)
)
Statement
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks agoByDDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks agoByDDD
Where to find the Crane Control Keycard in Atomfall
3 weeks agoByDDD
Assassin's Creed Shadows - How To Find The Blacksmith And Unlock Weapon And Armour Customisation
1 months agoByDDD
Roblox: Dead Rails - How To Complete Every Challenge
3 weeks agoByDDD

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools