phpphp-7.2

How to show each value separately from Array in descending order?


Want to show values separately from an array by writing "Echo". Also for each "Echo" code, the values must go in descending order.

Here is the Code for Descending values:

<?php
$toplist = array(10, 15, 20, 13, 18, 21, 25);

rsort($toplist);

$length = count($toplist);
for($x = 0; $x < $length; $x++) {
    echo $toplist[$x];
    echo "<br>";
}
?>

And the Output appears together like this:

25
21
20
18
15
13
10

But I want all values separately in descending order by writing each "Echo" code.

For exaples:

   echo $toplist[$x1];

output: 25

   echo $toplist[$x2];

output: 21

   echo $toplist[$x3];

output: 20

etc. etc...

Solution

  • Once the value is reverse-sorted with rsort, you can access any value in the array like this:

    echo $toplist[0];
    echo $toplist[1];
    echo $toplist[2];
    echo $toplist[3];
    echo $toplist[4];
    echo $toplist[5];
    echo $toplist[6];