Question #723   Submitted by Answiki on 10/22/2021 at 07:24:57 PM UTC

In PHP, how to foreach in reverse order?

Answer   Submitted by Answiki on 10/22/2021 at 07:32:40 PM UTC

In PHP, the best option to process a foreach loop in reverse order is to reverse the array. it can be done with array_reverse().

$array = array('A', 'B', 'C', 'D');
              
foreach( array_reverse($array) as  $value ){
    echo $value."\n";
}

The above code displays the letters A, B, C and D in reverse order:

D
C
B
A

5 events in history
Answer by Answiki on 10/22/2021 at 07:32:40 PM

In PHP, the best option to process a foreach loop in reverse order is to reverse the array. it can be done with array_reverse().

$array = array('A', 'B', 'C', 'D');
              
foreach( array_reverse($array) as  $value ){
    echo $value."\n";
}

The above code displays the letters A, B, C and D in reverse order:

D
C
B
A

Question by Answiki 10/22/2021 at 07:24:57 PM
In PHP, how to foreach in reverse order?
Question by Answiki 10/22/2021 at 07:24:37 PM
Foreach in reverse order in PHP?
Question by Answiki 10/22/2021 at 07:24:24 PM
How to process foreach in reverse order in PHP?
Question by Answiki 10/22/2021 at 07:23:59 PM
How to reverse order foreach in PHP?
# ID Query URL Count

Icons proudly provided by Friconix.