php数组/多维排序

时间:2017-02-10 07:59:23

标签: php arrays multidimensional-array

我有这个数组index[0]是得分,我正确排序。我坚持index[3]这是一些重要的数据。因为我希望它从index[0]从高到低,相应的index[3]从低到高。如你看到的 array (0)首先显示,但它包含分数20和index[3]的404,而array (1)的分数index[3]较低。请帮助谢谢。

[0] => Array
    (
        [0] => 20
        [1] => Revelyn Nazar
        [2] => PASSED
        [3] => 404
    )

[1] => Array
    (
        [0] => 20
        [1] => Mark Valle
        [2] => PASSED
        [3] => 351
    )

[2] => Array
    (
        [0] => 20
        [1] => Marita Serrano
        [2] => PASSED
        [3] => 372
    )

[3] => Array
    (
        [0] => 20
        [1] => Ma Lourdes Pulumbarit
        [2] => PASSED
        [3] => 482
    )

[4] => Array
    (
        [0] => 20
        [1] => Diana Rose Reyes
        [2] => PASSED
        [3] => 584
    )

[5] => Array
    (
        [0] => 20
        [1] => Andrea Reyes
        [2] => PASSED
        [3] => 398
    )

[6] => Array
    (
        [0] => 19
        [1] => Willie Masiclat
        [2] => PASSED
        [3] => 455
    )

[7] => Array
    (
        [0] => 19
        [1] => Rhaymond Emata
        [2] => PASSED
        [3] => 540
    )

[8] => Array
    (
        [0] => 19
        [1] => Magnolia Grace Mallari
        [2] => PASSED
        [3] => 516
    )

[9] => Array
    (
        [0] => 19
        [1] => Ma. Milania Castro
        [2] => PASSED
        [3] => 429
    )

[10] => Array
    (
        [0] => 19
        [1] => Kris Gutierrez
        [2] => PASSED
        [3] => 459
    )

[11] => Array
    (
        [0] => 19
        [1] => Karren Ann Cruz
        [2] => PASSED
        [3] => 410
    )

[12] => Array
    (
        [0] => 19
        [1] => Iwee Boy Sarita
        [2] => PASSED
        [3] => 451
    )

[13] => Array
    (
        [0] => 19
        [1] => Gretchen Concepcion
        [2] => PASSED
        [3] => 517
    )

[14] => Array
    (
        [0] => 19
        [1] => Clarissa Aguinaldo
        [2] => PASSED
        [3] => 439
    )

[15] => Array
    (
        [0] => 19
        [1] => Camille Jolo
        [2] => PASSED
        [3] => 347
    )

[16] => Array
    (
        [0] => 19
        [1] => April Buenaventura
        [2] => PASSED
        [3] => 600
    )

[17] => Array
    (
        [0] => 19
        [1] => Alyssa Rose Angelo
        [2] => PASSED
        [3] => 375
    )

[18] => Array
    (
        [0] => 18
        [1] => Joel Valencia
        [2] => PASSED
        [3] => 415
    )

[19] => Array
    (
        [0] => 18
        [1] => Errylyn Coronel
        [2] => PASSED
        [3] => 437
    )

[20] => Array
    (
        [0] => 18
        [1] => Editha Joy Paras
        [2] => PASSED
        [3] => 339
    )

[21] => Array
    (
        [0] => 18
        [1] => Diona Culala
        [2] => PASSED
        [3] => 601
    )

[22] => Array
    (
        [0] => 18
        [1] => Abbygael Aguirre
        [2] => PASSED
        [3] => 479
    )

2 个答案:

答案 0 :(得分:1)

您可以使用uasort按多个键进行排序

uasort($your_arr, function($a,$b){
    $c = $a[0] - $b[0];
    $c .= $a[3] - $b[3];
    return $c;
});

数组的结果

Array
(

[15] => Array
    (
        [0] => 19
        [1] => Camille Jolo
        [2] => PASSED
        [3] => 347
    )

[11] => Array
    (
        [0] => 19
        [1] => Karren Ann Cruz
        [2] => PASSED
        [3] => 410
    )

[9] => Array
    (
        [0] => 19
        [1] => Ma. Milania Castro
        [2] => PASSED
        [3] => 429
    )

[14] => Array
    (
        [0] => 19
        [1] => Clarissa Aguinaldo
        [2] => PASSED
        [3] => 439
    )

[12] => Array
    (
        [0] => 19
        [1] => Iwee Boy Sarita
        [2] => PASSED
        [3] => 451
    )

[6] => Array
    (
        [0] => 19
        [1] => Willie Masiclat
        [2] => PASSED
        [3] => 455
    )

[10] => Array
    (
        [0] => 19
        [1] => Kris Gutierrez
        [2] => PASSED
        [3] => 459 

答案 1 :(得分:0)

使用usort:

function my_sort($a,$b)
{
if ($a[0]==$b[0]){ return $a[3] - $b[3] };

return $a[0]-$b[0];
}

$a=/*your array*/;
usort($a,"my_sort");

请点击此处了解更多信息: http://www.w3schools.com/php/func_array_usort.asp