简体   繁体   English

如何按键值对数组排序并保持等号顺序

[英]How to sort array by key value and preserve order of equal

How do I sort an array by the value of a key and preserve the order if the value is equal. 如何通过键的值对数组进行排序并在值相等时保留顺序。

Array: 阵:

Array ( [0] => Array ( [id] => 65 [count] => 2 ) [1] => Array ( [id] => 67 [count] => 500 ) [2] => Array ( [id] => 61 [count] => 225 ) [3] => Array ( [id] => 58 [count] => 2 ) )

Desired output: 所需的输出:

Array ( [0] => Array ( [id] => 67 [count] => 500 ) [1] => Array ( [id] => 61 [count] => 225 ) [2] => Array ( [id] => 65 [count] => 2 ) [3] => Array ( [id] => 58 [count] => 2 ) )

I want to sort by the 'count' key from highest to lowest. 我想按“计数”键从最高到最低排序。

I'm guessing that we wish to double sort our array based on our two keys. 我猜想我们希望基于两个键对数组进行双重排序。 Then, we would define our array as $arr , and array_multisort on our desired keys: 然后,将我们的数组定义为$arr ,并在所需的键上定义array_multisort

array_multisort(array_column($arr, 'count'), SORT_DESC, array_column($arr, 'id'), SORT_DESC, $arr); 

If we are only sorting on count , we would remove id : 如果仅按count排序,则将删除id

array_multisort(array_column($arr, 'count'), SORT_DESC, $arr);

Test 1 测试1

$arr = [
    "0" =>
    [
        "id" => 65, "count" => 2,
    ],
    "1" =>
    [
        "id" => 67, "count" => 500,
    ],
    "2" =>
    [
        "id" => 61, "count" => 225,
    ],
    "3" =>
    [
        "id" => 58, "count" => 2,
    ],
];

array_multisort(array_column($arr, 'count'), SORT_DESC, array_column($arr, 'id'), SORT_DESC, $arr);

var_dump($arr);

Output 1 输出1

array(4) {
  [0]=>
  array(2) {
    ["id"]=>
    int(67)
    ["count"]=>
    int(500)
  }
  [1]=>
  array(2) {
    ["id"]=>
    int(61)
    ["count"]=>
    int(225)
  }
  [2]=>
  array(2) {
    ["id"]=>
    int(65)
    ["count"]=>
    int(2)
  }
  [3]=>
  array(2) {
    ["id"]=>
    int(58)
    ["count"]=>
    int(2)
  }
}

If we wish to sort only on count , this would likely work: 如果我们只希望对count进行排序,则可能会起作用:

Test 1 测试1

$arr = [
    "0" =>
    [
        "id" => 65, "count" => 2,
    ],
    "1" =>
    [
        "id" => 67, "count" => 500,
    ],
    "2" =>
    [
        "id" => 61, "count" => 225,
    ],
    "3" =>
    [
        "id" => 58, "count" => 2,
    ],
];

array_multisort(array_column($arr, 'count'), SORT_DESC, $arr);

var_dump($arr);

Output 2 输出2

  array(4) {
    [0]=>
    array(2) {
      ["id"]=>
      int(67)
      ["count"]=>
      int(500)
    }
    [1]=>
    array(2) {
      ["id"]=>
      int(61)
      ["count"]=>
      int(225)
    }
    [2]=>
    array(2) {
      ["id"]=>
      int(58)
      ["count"]=>
      int(2)
    }
    [3]=>
    array(2) {
      ["id"]=>
      int(65)
      ["count"]=>
      int(2)
    }
  }

You could use usort and return 0 if the values are the same to preserve the order: 您可以使用usort并返回0(如果值相同)以保留顺序:

$arrays = [
    ["id" => -10,"count" => 2],
    ["id" => 10000,"count" => 2],
    ["id" => 1000,"count" => -1],
    ["id" => 1000,"count" => 2],
    ["id" => 65,"count" => 2],
    ["id" => 67,"count" => 500],
    ["id" => 61,"count" => 225],
    ["id" => 58,"count" => 2],
    ["id" => 59,"count" => 2]
];

usort($arrays, function ($x, $y) {
    if ($x['count'] === $y['count']) {
        return 0;
    } elseif ($x['count'] > $y['count']) {
        return -1;
    } else {
        return 1;
    }
});

print_r($arrays);

Result 结果

Array
(
    [0] => Array
        (
            [id] => 67
            [count] => 500
        )

    [1] => Array
        (
            [id] => 61
            [count] => 225
        )

    [2] => Array
        (
            [id] => -10
            [count] => 2
        )

    [3] => Array
        (
            [id] => 10000
            [count] => 2
        )

    [4] => Array
        (
            [id] => 1000
            [count] => 2
        )

    [5] => Array
        (
            [id] => 65
            [count] => 2
        )

    [6] => Array
        (
            [id] => 58
            [count] => 2
        )

    [7] => Array
        (
            [id] => 59
            [count] => 2
        )

    [8] => Array
        (
            [id] => 1000
            [count] => -1
        )

)

See a php demo 看到一个PHP演示

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

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