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

廉价感情. 提交于 2019-12-13 07:15:19

问题


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>";
}


来源:https://stackoverflow.com/questions/38038026/how-to-apply-usort-with-two-criteria-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!