0

i've created multiple arrays just like this one:

<?php
    $student1 = array("name" => "dean", "score" => "10", "time" => "5");
?>

i've created 15 arrays just like the one above but with different values, after this i've created an array where i store the 15 arrays previously created just like this

<?php 
    $class = array($student1,$student2,....)
?>

now im asked to order $class from lower to higher depending on the score of the student

any help?

Sfili_81
  • 2,377
  • 8
  • 27
  • 36

2 Answers2

0

First, just make the combined array all at once instead of making a separate array per student.

Now, you just need to sort $class depending on score using usort.

Then, loop through the array and output the information to the page (based on your comment i just need to show the names and scores with the sort)

$class = array(
    array("name" => "dean", "score" => "10", "time" => "5"),
    array("name" => "student2", "score" => "5", "time" => "5"),
    array("name" => "student3", "score" => "8", "time" => "5")
);

usort($class, function($a, $b) {
    return $b['score'] - $a['score'];
});

//loop through the values to display
foreach($class as $key => $value) {
    
    //array keys start at 0, so add 1 to get the position on the leaderboard
    $position = $key + 1;

    echo "{$position}: {$value['name']} ({$value['score']})<br>";
}

Output

1: dean (10)
2: student3 (8)
3: student2 (5)
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
  • Thank you for your response, but when i put that code into mine, it doesn't do anything, the page shows blank, im kinda new to php. ill copy my code below: – Diogo Pereira Nov 12 '20 at 16:55
  • $class = array( array("nome" => "Diogo", "score" => "10 array("nome" => "Joana", "score" => "100", "time" => "6" ), array("nome" => "Diogo", "score" => "275", "time" => "4" ), array("nome" => "Francisco", "score" => "300", "time" => "9" ), ); usort($class, function($a,$b){ return $a['score'] - $b['score']; }); i've changed the code to portuguese that the logic is the same – Diogo Pereira Nov 12 '20 at 16:57
  • @DiogoPereira That's because I don't know how you want to actually handle the data or display it. All my code does is _sort_ it. It won't show anything on the page without you doing something additional. – GrumpyCrouton Nov 12 '20 at 16:58
  • i just need to show the names and scores with the sort, and then show the top 3, its like a leaderboard – Diogo Pereira Nov 12 '20 at 17:00
  • for example : 1: Dean: 900 2:Jean:300 3: John: 500 – Diogo Pereira Nov 12 '20 at 17:01
  • and after that show the remain students with their scores – Diogo Pereira Nov 12 '20 at 17:01
  • @DiogoPereira Output wasn't really a part of your question, but I updated my answer regardless. Try the new code, and note the change I made in the `usort` function to make it descending instead of ascending score. – GrumpyCrouton Nov 12 '20 at 17:06
  • Thank you so much, your saving lifes here xD, one more question, how would u show the only the top 3 below the list of students? – Diogo Pereira Nov 12 '20 at 17:17
  • @DiogoPereira You can just do the same loop again but add `if($position > 3) continue;` right above the `echo`. – GrumpyCrouton Nov 12 '20 at 17:20
  • it works amazingly, now i need to add some options, if there is a draw in terms of score the fastest time wins, if there is draw in score and time, then i need to order alphabetically – Diogo Pereira Nov 12 '20 at 18:26
  • @DiogoPereira This is getting way out of the realm of your original question, you should post a new question for this. – GrumpyCrouton Nov 12 '20 at 18:32
0
$student1 = array("name" => "dean", "score" => "10", "time" => "5");

$students = array(
      "Student1" => array("name" => "dean", "score" => "10", "time" => "5"),
      "Student2" => array());

foreach($students as $item) {
       sort($item[1]);
}

I think the best solution is by making a 2D-array for the $class, and looping through it.

Kahoot
  • 19
  • 1
  • This doesn't work. `$item[1]` doesn't refer to anything, and even if it did, it couldn't sort the `$students` array at all. – GrumpyCrouton Nov 12 '20 at 16:41