按两个用户定义的值对多维数组进行排序

Sorting multidimensional array by two user defined values

我需要按两个值对多维数组进行排序。

例如数组中有 4 个键。

Array(
    Array
    (
        [0] => 4B642D022980E5EBAA7CF4B6E1CC93769921CB42
        [1] => downloading
        [2] => Title
        [3] => 60
    )
    Array
    (
        [0] => 4B642D022980E5EBAA7CF4B6E1CC93769921CB42
        [1] => downloading
        [2] => Title
        [3] => 30
    )
    Array
    (
        [0] => 4B642D022980E5EBAA7CF4B6E1CC93769921CB42
        [1] => paused
        [2] => Title
        [3] => 30
    )
    Array
    (
        [0] => 4B642D022980E5EBAA7CF4B6E1CC93769921CB42
        [1] => completed
        [2] => Title
        [3] => 100
    )
)

有没有一种方法可以对数组进行排序,以便它对数组进行排序,首先完成键,然后是下载,然后是第三个暂停,然后还对包含下载和暂停的数组进行排序,从 100 下降到 0 3 键?

期望的输出是

Array(
    Array
    (
        [0] => 4B642D022980E5EBAA7CF4B6E1CC93769921CB42
        [1] => completed
        [2] => Title
        [3] => 100
    )
    Array
    (
        [0] => 4B642D022980E5EBAA7CF4B6E1CC93769921CB42
        [1] => downloading
        [2] => Title
        [3] => 60
    )
    Array
    (
        [0] => 4B642D022980E5EBAA7CF4B6E1CC93769921CB42
        [1] => downloading
        [2] => Title
        [3] => 30
    )
    Array
    (
        [0] => 4B642D022980E5EBAA7CF4B6E1CC93769921CB42
        [1] => paused
        [2] => Title
        [3] => 30
    )
)

uksort就是你所需要的。

这是一种让您定义自己的回调函数的类型。

uksort 随后使用此回调函数对数组重新排序。

您需要编写一个函数,根据两个条件对数组进行排序。

第一个是数组索引 1 处的字段的字母顺序(其中包含单词 completed、downloading...),如果出现平局,则您将使用数组索引 3 处的字段数组并按降序排序。

最后,您需要将创建的函数作为参数传递给 uksort。

希望对您有所帮助! :)

    $array = [
        [
            '4B642D022980E5EBAA7CF4B6E1CC93769921CB42',
            'downloading',
            'Title',
            '60',
        ],
        [
            '4B642D022980E5EBAA7CF4B6E1CC93769921CB42',
            'downloading',
            'Title',
            '30',
        ],
        [
            '4B642D022980E5EBAA7CF4B6E1CC93769921CB42',
            'paused',
            'Title',
            '30',
        ],
        [
            '4B642D022980E5EBAA7CF4B6E1CC93769921CB42',
            'completed',
            'Title',
            '100',
        ]
    ];


    usort($array, function($a, $b) {
        if ($a[1] == $b[1]) {
            if ($a[3] == $b[3]) {
                return 0;
            }

            return ($a[3] > $b[3]) ? -1 : 1;
        }

        $status = ['completed', 'downloading', 'paused'];

        foreach ($status as $st) {
            if ($a[1] == $st)
                return -1;
            if ($b[1] == $st)
                return 1;
        }

        return 0;
    });


    var_dump($array);

您需要重新格式化数组并使用 array_multisort。它还将使想法更具可读性:

<?php $ar = Array(
    Array(
        "id" => "4B642D022980E5EBAA7CF4B6E1CC93769921CB42",
        "status" => "completed",
        "title" => "Title",
        "rank" => "100",
    ),
    Array
    (
        "id" => "4B642D022980E5EBAA7CF4B6E1CC93769921CB42",
        "status" => "downloading",
        "title" => "Title",
        "rank" => "60",
    ),
    Array
    (
        "id" => "4B642D022980E5EBAA7CF4B6E1CC93769921CB42",
        "status" => "downloading",
        "title" => "Title",
        "rank" => "30",
    ),
    Array
    (
        "id" => "4B642D022980E5EBAA7CF4B6E1CC93769921CB42",
        "status" => "paused",
        "title" => "Title",
        "rank" => "30",
    ),
);
var_dump($ar);
foreach ($ar as $key => $row) {
    $status[$key] = $row['status'];
    $rank[$key] = $row['rank'];
}

array_multisort($status, SORT_ASC, $rank, SORT_DESC, $ar);
var_dump($ar);