This function reverses the order of the elements of an array. Think of using foreach() on an array; there’s a certain order it goes through. A “reversed” array will have a reversed order.

Use the second parameter to preserve the numeric keys. The default is false meaning it will be re-indexed.

$arr = array('apple',
             'banana',
             'carrot',
             'a' => 'zebra',
             'b' => 'yak');
 
$new = array_reverse($arr);
/*
array (
  'b' => 'yak',
  'a' => 'zebra',
  0 => 'carrot',
  1 => 'banana',
  2 => 'apple'
)
*/
 
$new = array_reverse($arr, true);
/*
array (
  'b' => 'yak',
  'a' => 'zebra',
  2 => 'carrot',
  1 => 'banana',
  0 => 'apple'
)
*/

Note the numeric keys in the example above. Nested arrays are not affected as shown below. That is, the elements of the nested arrays are left alone.

$arr = array('apple',
             'banana',
             'carrot',
             array('one', 'two'),
             'a' => 'zebra',
             'b' => 'yak');
 
$new = array_reverse($arr);
/*
array (
  'b' => 'yak',
  'a' => 'zebra',
  0 => 
  array (
    0 => 'one',
    1 => 'two',
  ),
  1 => 'carrot',
  2 => 'banana',
  3 => 'apple'
)
*/