Current Location: Home> Latest Articles> How to Use PHP's array_reverse Function to Reverse the Order of Array Elements

How to Use PHP's array_reverse Function to Reverse the Order of Array Elements

M66 2025-06-04

Using PHP's array_reverse Function to Reverse the Order of Array Elements

PHP, as a powerful scripting language, is not only used for web development but also efficiently handles various data structures, especially arrays. This article will take you through how to use PHP's built-in function "array_reverse" to reverse the order of elements in an array.

What is the array_reverse Function?

"array_reverse" is an array manipulation function in PHP that reverses the order of elements in an array. Below is the basic syntax of the function:

array array_reverse ( array $array [, bool $preserve_keys = FALSE ] )

Function Parameter Explanation

  • $array: The array whose order you want to reverse.
  • $preserve_keys: An optional boolean value that determines whether to preserve the keys of the array. The default is false, meaning that the reversed array will not retain the original keys; if set to true, the keys will remain unchanged.

Example Code

Now, let's demonstrate how to use the "array_reverse" function to reverse the order of elements in an array with a simple example.

<?php
// Create a simple array
$fruits = array("apple", "banana", "orange", "grape");

// Output the original array
echo "Original Array:";
print_r($fruits);

// Use array_reverse to reverse the array
$reversed_fruits = array_reverse($fruits);

// Output the reversed array
echo "Reversed Array:";
print_r($reversed_fruits);
?>

In the code above, we first create an array containing the names of fruits. Then, we use the "array_reverse" function to reverse the order of the array elements, storing the result in the new variable "$reversed_fruits." Finally, we print both the original and reversed arrays.

Execution Results

When running the above code, the output will appear as follows:

Original Array:
Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => grape
)

Reversed Array:
Array
(
    [0] => grape
    [1] => orange
    [2] => banana
    [3] => apple
)

As shown above, the "array_reverse" function successfully reverses the order of the elements in the original array.

Preserving Keys

If you want to preserve the keys of the array when reversing the order, you can set the second parameter to true:

$reversed_fruits = array_reverse($fruits, true);

By doing so, the array keys will not be reset, and the original keys will remain intact.

Summary

Through the example above, you have learned how to use PHP's "array_reverse" function to reverse the order of array elements. Whether it's a simple indexed array or an associative array, this function can reverse the order of elements. Additionally, you can choose whether or not to preserve the original array keys when using this function.

We hope this article helps you efficiently handle arrays in PHP development and enhances your skills in array manipulation.