How to apply usort() with two criteria in PHP?

2019-09-13 12:13发布

问题:

My array looks like this

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
        )

)

The outcome is this

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

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

回答1:

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

(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 is much faster than usort - on a 100k array dataset, about 30x faster.



回答2:

Here's one solution.

function compare($array1,$array2)
{
  // if gender is equal we need to look at the last name
  if ($array1['gender'] == $array2['gender'])
  {
    // so we look at the last name
    return strcasecmp($array1['last_name'],$array2['last_name']);
  }
  // this happens to work for gender, F is before M
  return strcasecmp($array1['gender'],$array2['gender']);
}

$data = [['last_name'      => 'Bonk',
          'first_name'     => 'Radek',
          'gender'         => 'Male',
          'date_of_birth'  => '6/3/1975',
          'favorite_color' => 'Green'],

         ['last_name'      => 'Seles',
          'first_name'     => 'Monica',
          'gender'         => 'Female',
          'date_of_birth'  => '12/2/1973',
          'favorite_color' => 'Black'],

         ['last_name'      => 'Abercrombie',
          'first_name'     => 'Neil',
          'gender'         => 'Male',
          'date_of_birth'  => '2/13/1943',
          'favorite_color' => 'Tan']];

usort($data,'compare');

foreach ($data as $key => $value)
{
  echo "<pre>$key: ".print_r($value,TRUE)."\n</pre>";
}