Definition and Usage
- PHP Version
- 4+
The array_reverse() function returns a new array with the elements of a given array in reverse order.
Features
- This function returns the elements of a given array in reverse order.
- It returns a new array without modifying the original array.
- The first element becomes the last, and the last element becomes the first.
- For associative arrays that use string keys, the order of values is reversed, but the association between keys and values is preserved.
Basic Examples
In the basic examples below, we will look at both indexed arrays and associative arrays to understand how array_reverse() works.
Indexed Array
$array = [1, 2, 3, 4, 5];
// Reverses the order of elements in the array.
print_r(array_reverse($array));
/* Output:
Array (
[0] => 5
[1] => 4
[2] => 3
[3] => 2
[4] => 1
)
*/
// The original array is not modified.
print_r($array);
/* Output:
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
*/
Associative Array
When the array_reverse() function is applied to an associative array, the order of values is reversed, but the association between string keys and values is preserved.
$associative_array = [
'name' => 'John Doe',
'age' => 30,
'city' => 'New York'
];
// Reverses the order of elements in the array.
// The association between string keys and values is preserved.
print_r(array_reverse($associative_array));
/* Output:
Array (
[city] => New York
[age] => 30
[name] => John Doe
)
*/
// The original array is not modified.
print_r($associative_array);
/* Output:
Array (
[name] => John Doe
[age] => 30
[city] => New York
)
*/
Syntax
array_reverse(array $array, bool $preserve_keys = false): array
Parameters
$array |
The array whose elements are to be reversed. |
|---|---|
$preserve_keys |
Optional. The default value is false.
|
$preserve_keys Parameter
The following example demonstrates how the $preserve_keys setting works.
$original_array = ['apple', 'banana', 'cherry', 'date'];
// preserve_keys is not specified (default is false)
$reversed_array_default = array_reverse($original_array);
print_r($reversed_array_default);
/* Output:
Array (
[0] => date
[1] => cherry
[2] => banana
[3] => apple
)
*/
// preserve_keys is set to true
$reversed_array_preserved = array_reverse($original_array, true);
print_r($reversed_array_preserved);
/* Output:
Array (
[3] => date
[2] => cherry
[1] => banana
[0] => apple
)
*/
When the $preserve_keys parameter is set to true, the original numeric keys are preserved while the order of array elements is reversed.
This can be useful when you need to reverse the order of an array while preserving its original structure. It provides greater flexibility and allows the function to be used in a wider range of situations.
Return Values
Returns a new array with the order of elements in the given array reversed.
Practical Examples
This function can be used in a variety of situations. The following are some common use cases.
Reversing the Order of Array Elements
This is used when the order of elements in a given array needs to be reversed. For example, it can be used to display user-input data in reverse order or to show the most recently added items first in an array.
$original_array = ['apple', 'banana', 'cherry', 'date'];
// Output the array with reversed order
print_r(array_reverse($original_array));
/* Output:
Array (
[0] => date
[1] => cherry
[2] => banana
[3] => apple
)
*/
Reversing Associative Array Order
This is used to reverse the order of key-value pairs in an associative array. It can be useful for displaying recently added items first or reversing the order of elements based on a specific requirement.
$associative_array = [
'name' => 'John Doe',
'age' => 30,
'city' => 'New York'
];
// Output the reversed associative array
print_r(array_reverse($associative_array));
/* Output:
Array (
[city] => New York
[age] => 30
[name] => John Doe
)
*/