本文介绍了如何在PHP中应用带有两个条件的usort()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数组看起来像这样

Array
(
    [0] => Array
        (
            [last_name] => Kournikova
            [first_name] => Anna
            [gender] => Female
            [date_of_birth] => 6/3/1975
            [favorite_color] => Red
        )

    [1] => Array
        (
            [last_name] =>  Hingis
            [first_name] => Martina
            [gender] => Female
            [date_of_birth] => 4/2/1979
            [favorite_color] => Green
        )

    [2] => Array
        (
            [last_name] =>  Seles
            [first_name] => Monica
            [gender] => Female
            [date_of_birth] => 12/2/1973
            [favorite_color] => Black
        )

    [3] => Array
        (
            [last_name] => Abercrombie
            [first_name] =>  Neil
            [gender] =>  Male
            [date_of_birth] =>  2/13/1943
            [favorite_color] =>  Tan
        )

    [4] => Array
        (
            [last_name] => Bishop
            [first_name] =>  Timothy
            [gender] =>  Male
            [date_of_birth] =>  4/23/1967
            [favorite_color] =>  Yellow
        )

    [5] => Array
        (
            [last_name] =>  Kelly
            [first_name] =>  Sue
            [gender] =>  Female
            [date_of_birth] =>  7/12/1959
            [favorite_color] =>  Pink
        )

    [6] => Array
        (
            [last_name] => Smith
            [first_name] => Steve
            [gender] => Male
            [date_of_birth] => 3/3/1985
            [favorite_color] => Red
        )

    [7] => Array
        (
            [last_name] => Bonk
            [first_name] => Radek
            [gender] => Male
            [date_of_birth] => 6/3/1975
            [favorite_color] => Green
        )

    [8] => Array
        (
            [last_name] => Bouillon
            [first_name] => Francis
            [gender] => Male
            [date_of_birth] => 6/3/1975
            [favorite_color] => Blue
        )

)

结果就是这个

Kournikova Anna Female 6/3/1975 Red
Hingis Martina Female 4/2/1979 Green
Seles Monica Female 12/2/1973 Black
Abercrombie Neil Male 2/13/1943 Tan
Bishop Timothy Male 4/23/1967 Yellow
Kelly Sue Female 7/12/1959 Pink
Smith Steve Male 3/3/1985 Red
Bonk Radek Male 6/3/1975 Green
Bouillon Francis Male 6/3/1975 Blue

我希望结果以姓氏升序在男性之前是女性

I want the outcome to be Females before Males by last_name ascending

Hingis Martina Female 4/2/1979 Green
Kelly Sue Female 7/12/1959 Pink
Kournikova Anna Female 6/3/1975 Red
Seles Monica Female 12/2/1973 Black
Abercrombie Neil Male 2/13/1943 Tan
Bishop Timothy Male 4/23/1967 Yellow
Bonk Radek Male 6/3/1975 Green
Bouillon Francis Male 6/3/1975 Blue
Smith Steve Male 3/3/1985 Red

如何应用usort()按该顺序对它们进行排序?

How can I apply usort() to sort them by that order?

推荐答案

按女性先于男性排序,然后按姓氏ASC排序:

Sort by females befores males, and then by last_name ASC:

(假设您的数组包含在$data中)

(assuming your array is contained in $data)

array_multisort(
    array_column($data, 'gender'), SORT_ASC,
    array_column($data, 'last_name'), SORT_ASC,
    $data
);

array_multisort比usort快得多-在100k数组数据集上,快30倍.

array_multisort is much faster than usort - on a 100k array dataset, about 30x faster.

这篇关于如何在PHP中应用带有两个条件的usort()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 21:05