PHP-按点排序,然后按字母顺序

时间:2018-07-15 09:23:28

标签: php sorting

我有一个数组:

Array (
    [0] => Array
       ( [points] => 10
         [id] => 58
         [nazwa] => auser1 )
    [1] => Array
       ( [points] => 15
         [id] => 36
         [nazwa] => cuser2 )
    [2] => Array 
       ( [points] => 15
         [id] => 57
         [nazwa] => buser3 )
    [3] => Array
       ( [points] => 20
         [id] => 56
         [nazwa] => duser4 )
    [4] => Array
       ( [points] => 20
         [id] => 54
         [nazwa] => euser5 ))

我想按points对该数组排序,然后按nazwa的字母顺序排序。

我该怎么做?

我想为俄罗斯杯创建决赛桌!

1 个答案:

答案 0 :(得分:1)

如果要按顺序对多维数组进行排序,然后按名称排序,则必须以相同的序列格式创建多维数组 例如:第一个元素应该是点,第二个名字,姓氏。请参考以下顺序。

 $array = [ [ 'points' => 10, 'nazwa' => 'auser1', 'id' => 58 ],
    [ 'points' => 15, 'nazwa' => 'cuser2', 'id' => 36 ],
    [ 'points' => 15, 'nazwa' => 'buser3', 'id' => 57 ],
    [ 'points' => 20, 'nazwa' => 'duser4', 'id' => 56 ],
    [ 'points' => 20, 'nazwa' => 'euser5', 'id' => 54 ]];

    array_multisort( $array );
    print_r(($array));

输出:

Array
(
    [0] => Array
        (
            [points] => 10
            [nazwa] => auser1
            [id] => 58
        )

    [1] => Array
        (
            [points] => 15
            [nazwa] => buser3
            [id] => 57
        )

    [2] => Array
        (
            [points] => 15
            [nazwa] => cuser2
            [id] => 36
        )

    [3] => Array
        (
            [points] => 20
            [nazwa] => duser4
            [id] => 56
        )

    [4] => Array
        (
            [points] => 20
            [nazwa] => euser5
            [id] => 54
        )

)