Forum : How do I reverse the order of the elements in an array?
Brief description  about Online courses   join in Online courses
View prabeen  patra 's Profile

How do I reverse the order of the elements in an array?

How do I reverse the order of the elements in an array??????
Asked by prabeen patra | Mar 17, 2010 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
Hi Prabeen,
This is easy to do in PHP - as with most common things you might like to do with an array, there is already a nice PHP function to take the strain for you. So no need to concoct your own function - just use the built-in power of PHP to do it for you, a little like this:

<?php
$countup = range(1,5);
print_r($countup);
$countdown = array_reverse($countup);
print_r($countdown);
?>
This will output the following:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 2
[4] => 1
)
Mar 17, 2010