1

I have array echo this: (2 5 7 13 19 22 23 37 41 41 64 74 85 96 139);
But I need this echo: (139 96 85 74 64 41 41 37 23 22 19 13 7 5 2);
I can't find, how to upside down echo value?

$num= array(7,13,85,64,2,41,22,96,139,37,41,19,74,23,5);
$max= max($num);
$a= count($num);
sort($num);

for ($x=0; $x < $a; $x++) {
        echo $num[$x]. " ";
}

output: 2 5 7 13 19 22 23 37 41 41 64 74 85 96 139

2
  • for ( $x=($a-1); $x >= 0; $x—) Commented Mar 16, 2019 at 12:44
  • Start from the final index and loop backwards. Commented Mar 16, 2019 at 12:45

3 Answers 3

5

Replace sort($num); with rsort($num);.

sort() sorts from low to high.

rsort() sorts from high to low.

Sign up to request clarification or add additional context in comments.

Comments

0

use array rsort()

$output=rsort($num);

Comments

0

If array is already sorted, then use array_reverse. It is more performant than sorting array in descending order.

$reversedOrderArray = array_reverse($yourArray);

2 Comments

$num[$x] is not array, $num is.
Oh yeah, I have found it, it's working! Thanks dude. :)

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.