按两个键排序数组

时间:2014-08-23 15:54:17

标签: php arrays sorting

我有一个带有几个名字的数组。

$array = Array
(

    [0] => Array
        (
            [fullname] => Skyler White
            [fname] => Skyler
            [lname] => White
        )

    [1] => Array
        (
            [fullname] => Walter White
            [fname] => Walter
            [lname] => White
        )

    [2] => Array
        (
            [fullname] => Marie Schrader
            [fname] => Marie
            [lname] => Schrader
        )

    [3] => Array
        (
            [fullname] => Hank Schrader
            [fname] => Hank
            [lname] => Schrader
        )

    [4] => Array
        (
            [fullname] => Jesse Pinkman
            [fname] => Jesse
            [lname] => Pinkman
        )

)

我希望数组排序符合以下条件:

1)姓氏lname 2)名字fname

所以有序数组看起来像:

$array_ordered = Array
(

    [0] => Array
        (
            [fullname] => Jesse Pinkman
            [fname] => Jesse
            [lname] => Pinkman
        )

    [1] => Array
        (
            [fullname] => Hank Schrader
            [fname] => Hank
            [lname] => Schrader
        )

    [2] => Array
        (
            [fullname] => Marie Schrader
            [fname] => Marie
            [lname] => Schrader
        )


    [3] => Array
        (
            [fullname] => Skyler White
            [fname] => Skyler
            [lname] => White
        )

    [4] => Array
        (
            [fullname] => Walter White
            [fname] => Walter
            [lname] => White
        )

)

1 个答案:

答案 0 :(得分:2)

您可以使用usort和回调函数进行比较,使用strnatcmp

function compare($a, $b)
{
    $retLastName = strnatcmp($a['lname'], $b['lname']);
    if(!$retLastName) 
         $retLastName = strnatcmp($a['fname'], $b['fname']);
    return $retLastName;
}

usort($array, 'compare');

@edit

为了解释strnatcmp的用法,我将展示一个小例子,我认为它不言自明。

function compareStrcmp($a, $b){
   $retLastName = strcmp($a['lname'], $b['lname']);
   if(!$retLastName) 
        $retLastName = strcmp($a['fname'], $b['fname']);
   return $retLastName;
}

function compare($a, $b){
   $retLastName = strnatcmp($a['lname'], $b['lname']);
   if(!$retLastName) 
        $retLastName = strnatcmp($a['fname'], $b['fname']);
   return $retLastName;
}

$arr1 = $arr2 = array(
       0 => array (
            "fullname" => "Walter White",
            "fname" => "Walter",
            "lname" => "White"
       ) , 
       1 => array (
            "fullname" => "Marie Schrader",
            "fname" => "Marie",
            "lname" => "Schrader"
       ) , 
       2 => array (
            "fullname" => "Walter White 1",
            "fname" => "Walter",
            "lname" => "White 1"
       ) ,
       3 => array (
            "fullname" => "Walter White 10",
            "fname" => "Walter",
            "lname" => "White 10"
       ) ,
       4 => array (
            "fullname" => "Walter White 2",
            "fname" => "Walter",
            "lname" => "White 2"
       )
);
echo "Standard string comparison\n";

usort($arr1, 'compareStrcmp');
print_r($arr1);

echo "\nNatural order string comparison\n";
usort($arr2, "compare");
print_r($arr2);

输出:

Standard string comparison
Array
(
    [0] => Array
        (
            [fullname] => Marie Schrader
            [fname] => Marie
            [lname] => Schrader
        )

    [1] => Array
        (
            [fullname] => Walter White
            [fname] => Walter
            [lname] => White
        )

    [2] => Array
        (
            [fullname] => Walter White 1
            [fname] => Walter
            [lname] => White 1
        )

    [3] => Array
        (
            [fullname] => Walter White 10
            [fname] => Walter
            [lname] => White 10
        )

    [4] => Array
        (
            [fullname] => Walter White 2
            [fname] => Walter
            [lname] => White 2
        )

)

Natural order string comparison
Array
(
    [0] => Array
        (
            [fullname] => Marie Schrader
            [fname] => Marie
            [lname] => Schrader
        )

    [1] => Array
        (
            [fullname] => Walter White
            [fname] => Walter
            [lname] => White
        )

    [2] => Array
        (
            [fullname] => Walter White 1
            [fname] => Walter
            [lname] => White 1
        )

    [3] => Array
        (
            [fullname] => Walter White 2
            [fname] => Walter
            [lname] => White 2
        )

    [4] => Array
        (
            [fullname] => Walter White 10
            [fname] => Walter
            [lname] => White 10
        )

)

正如你所看到的,strcmp将'Walter White 10'置于'Walter White 2'之前,这对我们人类可能是错误的。另一方面,Strnatcmp不会这样做,并正确显示它。

欲了解更多信息:

PHP example

Natsort algorithm