简体   繁体   English

PHP按两个条件对数组进行排序

[英]PHP Sort array by two criteria

I have an array like this: 我有一个像这样的数组:

$arr = [
    0 => [
        'a' => 0,
        'b' => 150
    ],
    1 => [
        'a' => 1,
        'b' => 155
    ],
    2 => [
        'a' => 0,
        'b' => 131
    ],
    3 => [
        'a' => 2,
        'b' => 241
    ],
    4 => [
        'a' => 2,
        'b' => 45
    ],
    5 => [
        'a' => 1,
        'b' => 23
    ],
    6 => [
        'a' => 0,
        'b' => 78
    ],
    7 => [
        'a' => 0,
        'b' => 123
    ],
    8 => [
        'a' => 1,
        'b' => 412
    ],
    9 => [
        'a' => 0,
        'b' => 22
    ],
    10 => [
        'a' => 0,
        'b' => 11
    ],
    11 => [
        'a' => 2,
        'b' => 100
    ],
    12 => [
        'a' => 2,
        'b' => 105
    ],
    13 => [
        'a' => 1,
        'b' => 18
    ]
];

I want to sort the array first by the 'a' property, so all 0s, all 1s and all 2s should be together, and then all the 'b's should be ordered from lowest to highest. 我想首先通过'a'属性对数组进行排序,因此所有0,所有1和所有2应该在一起,然后所有'b'应该从最低到最高排序。 I hope you know what I mean. 我希望你明白我的意思。 I could separate it into three arrays and then sort each one and then put them back together but I hope there's a shorter and better solution. 我可以将其分为三个数组,然后对每个数组进行排序,然后将它们放回一起,但我希望有一个更短,更好的解决方案。

Just wanted to explain why the thing you originally tried didn't work. 只是想说明为什么您最初尝试的操作不起作用。

return a['a'] - b['a'] || a['b'] - b['b']

|| is a logical operator , so when you use that the entire expression will be evaluated as a boolean, so the only possible values are true or false (or numerically, as the usort comparison function should return, 0 or 1). 逻辑运算符 ,因此当您使用整个表达式将被评估为布尔值时,因此唯一可能的值是truefalse (或数值,因为usort比较函数应返回0或1)。 Since none of the comparisons ended up with zeroes on both sides of the || 由于没有一个比较结束于||两侧都为零 , all of them evaluated to true , so no sorting happened. ,所有这些对象都评估为true ,因此没有排序。

Using ?: instead worked because it doesn't evaluate the whole expression as a boolean, it evaluates the first part and returns the result of that, unless it's zero (equal values) in which case it goes on to evaluate the second part and return the result of that. 使用?:而是有效的,因为它没有将整个表达式评估为布尔值,而是评估了第一部分并返回了结果,除非它为零(等于值),在这种情况下它将继续评估第二部分并返回结果。

Example showing the difference between the two comparisons at 3v4l.org: https://3v4l.org/32IPa 在3v4l.org上显示两次比较之间差异的示例: https ://3v4l.org/32IPa


You can actually sort that array more simply, though. 但是,您实际上可以更简单地对该数组进行排序。

sort($arr);

will work. 将工作。 You can't always count on that working for sorting arrays, though. 但是,您不能总是指望它能对数组进行排序。 It just works in this case because all the inner arrays have the same keys, and you're wanting to sort by the first key, then the second. 它仅在这种情况下起作用,因为所有内部数组都具有相同的键,并且您要按第一个键排序,然后按第二个键排序。 (You can see how arrays are compared in "Example #2 Transcription of standard array comparison" here .) (您可以在此处的 “示例#2标准数组比较的转录”中了解如何比较数组。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM