简体   繁体   中英

php array / multidimensional sort

I have this array index[0] was the score and I sorted it correctly. and I'm stuck at index[3] which is some important data. coz I want it to from index[0] from high to low with the corresponding sort of index[3] from low to high. as you can see array (0) display first but it contains the score of 20 and index[3] of 404 instead array (1) had a lower index[3] . please help thanks.

[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
    )

)

You can use uasort for sorting by more than 1 keys

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

Result from your array

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 

Use 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");

see here for more info: http://www.w3schools.com/php/func_array_usort.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM