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

时间:2019-06-03 23:02:19

标签: php arrays sorting

如何按键的值对数组排序,如果值相等,则保留顺序。

数组:

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 ) )

所需的输出:

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 ) )

我想按“计数”键从最高到最低排序。

2 个答案:

答案 0 :(得分:0)

我猜想我们希望根据两个键对数组进行双重排序。然后,将数组定义为$arr,并在所需键上定义array_multisort

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

如果仅对count进行排序,则会删除id

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

测试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);

输出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)
  }
}

如果我们只希望对count进行排序,则可能会起作用:

测试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);

输出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)
    }
  }

答案 1 :(得分:0)

您可以使用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);

结果

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
        )

)

查看php demo