0

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
    )

)

3
  • can you help me a lil' code co'z i really stuck here. i sorted correctly at score but when i included index[3] it got me stuck. sorting array using two index made me drain. thanks. Commented Feb 10, 2017 at 8:11
  • your question is unclear, please debug the code and attach error messages if any. Commented Feb 10, 2017 at 8:20
  • there's no error. all i need is to sort it via score and index[3]. thanks. Commented Feb 10, 2017 at 8:25

2 Answers 2

1

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 
Sign up to request clarification or add additional context in comments.

2 Comments

i'd got a nice result. but i want to be the highest score with lowest index[3] will be on the first array. thanks for your help.
Good to know that you got result. Happy coding
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.