按一列对二维数组中的行进行排序,同时保留原始的列关系
我的数组如下所示:
Array
(
[0] => Array
(
[0] => 1
[1] => 6
[2] => 4
[3] => 5
)
[1] => Array
(
[0] => 272.05
[1] => 63.54
[2] => 544.79
[3] => 190.62
)
[2] => Array
(
[0] => 2011-03-06 14:08:19
[1] => 2011-03-06 14:29:04
[2] => 2011-03-06 14:28:39
[3] => 2011-03-06 14:29:28
)
)
我想按 $myArray[1] 排序。我有这个 usort 函数:
function sortAmount($a, $b) {
return strnatcmp($a[1], $b[1]);
}
它是这样调用的:
usort($myArray, "sortAmount");
但是,调用 usort 后数组不会改变。我希望 $myArray[1] 中的数字按升序排序,并且 $myArray[0] 和 $myArray[2] 中的相应索引随之更改。
My array looks like this:
Array
(
[0] => Array
(
[0] => 1
[1] => 6
[2] => 4
[3] => 5
)
[1] => Array
(
[0] => 272.05
[1] => 63.54
[2] => 544.79
[3] => 190.62
)
[2] => Array
(
[0] => 2011-03-06 14:08:19
[1] => 2011-03-06 14:29:04
[2] => 2011-03-06 14:28:39
[3] => 2011-03-06 14:29:28
)
)
I want to sort by $myArray[1]. I have this usort function:
function sortAmount($a, $b) {
return strnatcmp($a[1], $b[1]);
}
It is called like this:
usort($myArray, "sortAmount");
However, the array does not change after calling usort. I want the numbers in $myArray[1] to be sorted in ascending order, and for the corresponding indexes in $myArray[0] and $myArray[2] to change with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你想要
array_multisort
:给出
Apart由此看来,为什么要使用
strcmp
来比较数字?I think you want
array_multisort
:gives
Apart from that, why do you use
strcmp
to compare numbers?