在PHP中按频率对元素进行排序

时间:2018-07-17 11:55:55

标签: php arrays sorting frequency

输入:

$result = Array
    (
        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),

        Array
            (
                "name" => "ლუკა ქურასბედიანი",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "z",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "z",
            ),

       Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "z",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "z",
            ), 
        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),
        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),
        Array(
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "z",
            ),

    );

我想要按频率对元素进行排序(通过“ parrent_club_hash_id”)。

11-x 7-y5-z,所以我想要这样的输出:

$result = Array
    (
        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "ლუკა ქურასბედიანი",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

       Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "x",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "y",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "z",
            ),

        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "z",
            ), 
        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "z",
            ),
        Array
            (
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "z",
            ),
        Array(
                "name" => "",
                "squad_member_hash_id" => "",
                "parrent_club_hash_id" => "z",
            ),

    );

1 个答案:

答案 0 :(得分:0)

首先获取您的值的频率:

$values = array_count_values(array_column($result, 'parrent_club_hash_id'));

然后使用自定义排序按这些频率进行排序:

usort($result, function ($x, $y) use ($values) {
    // $y is first because you need descending sort
    return $values[$y["parrent_club_hash_id"]] - $values[$x["parrent_club_hash_id"]];
});

示例:http://sandbox.onlinephpfunctions.com/code/6265192e677641afbc21e899d97f252c14b5402f