0
Hello to all recently wrote this code, where it will show the list of students in the array by decreasing order of score and at the end shows the top 3. This is the code:
<?php
$Turma = array(
array("nome" => "Diogo", "score" => "100", "time" => "6" ),
array("nome" => "Joao","score" => "500", "time" => "3" ),
array("nome" => "Miguel", "score" => "125", "score" => "8" ),
array("nome" => "Daniela", "score" => "105", "time" => "7" ),
array("nome" => "Joana", "score" => "100", "time" => "6" ),
array("nome" => "Diogo", "score" => "275", "time" => "4" ),
array("nome" => "Francisco", "score" => "300", "time" => "9" ),
array("nome" => "Ines", "score" => "650", "time" => "2" ),
array("nome" => "Dionisio", "score" => "101", "score" => "10" ),
array("nome" => "Ricardo", "score" => "200", "score" => "8" ),
array("nome" => "Fabio", "score" => "201", "score" => "11" ),
array("nome" => "Tiago","score" => "50", "score" => "13" ),
array("nome" => "Carolina", "score" => "150", "time" => "5" ),
array("nome" => "Rui", "score" => "130", "time" => "3" ),
array("nome" => "Luisa", "score" => "1000", "time" => "1" ),
);
usort($Turma, function($a,$b){
return $b["score"] - $a["score"];
});
foreach($Turma as $key => $value) {
$position = $key + 1;
echo "{$position}: {$value['nome']} : {$value['score']} <br>";
}
echo "<br>";
echo "WINNERS!! <br> ";
foreach($Turma as $key => $value) {
$position = $key + 1;
if ($position < 4) {
echo "{$position}: {$value['nome']} : {$value['score']} <br>";
}
}
Now I’m asked to sort the students in case there’s a score draw the fastest wins, guy has a tie in time and score we have to sort alphabetically.
Good afternoon. Post the code instead of posting the image. Click edit.
– Wallace Maxters
To compare strings use
strcmp()
orstrcasecmp
. The difference between them is that in the first the comparison does not distinguish between upper and lower case– Augusto Vasques
What is the used version of PHP?
– Wallace Maxters