Robson » Little Red Book » Array reversal

Chapter 6 - Question 13

Given an array of values, process them so that their order in the array is reversed.

Solution 1

The PHP array_reverse function makes this a very simple problem.

<?
   // some values to be processed
   $values = array(9, 4, 3, 0, 1, 5);
   
   // show them in the normal order and reversed order
   echo 'Normal: ' . implode(', ', $values) . '<br/>' .
        'Reversed: ' . implode(', ', array_reverse($values));
?>

Which produces:

Normal: 9, 4, 3, 0, 1, 5
Reversed: 5, 1, 0, 3, 4, 9

Log

© 2004-17 robson | cc unless stated