Home >Backend Development >PHP Problem >How to reverse array values in php
How to reverse array values in php: You can use the array_reverse() function to achieve this. The syntax of the function is: [array_reverse(array,preserve)], where the parameter array specifies the array that needs to be reversed.
Recommended: "php video tutorial"
How to reverse php array values:
array_reverse()
The function returns an array in the reverse order of elements.
Description
array_reverse()
The function reverses the order of elements in the original array, creates a new array and returns it.
If the second parameter is specified as true, the key name of the element remains unchanged, otherwise the key name will be lost.
Syntax
array_reverse(array,preserve)
Parameters
array required. Specifies an array.
preserve
Optional. Specifies whether to retain the original array key names.
This parameter is newly added in PHP 4.0.3.
Possible values:
true
false
Examples:
Example 1
Return Original array, reversed array, flipped array retaining the original array key name:
<?php $a=array("Volvo","XC90",array("BMW","Toyota")); $reverse=array_reverse($a); $preserve=array_reverse($a,true); print_r($a); print_r($reverse); print_r($preserve); ?>
Run result:
Array ( [0] => Volvo [1] => XC90 [2] => Array ( [0] => BMW [1] => Toyota ) ) Array ( [0] => Array ( [0] => BMW [1] => Toyota ) [1] => XC90 [2] => Volvo ) Array ( [2] => Array ( [0] => BMW [1] => Toyota ) [1] => XC90 [0] => Volvo )
The above is the detailed content of How to reverse array values in php. For more information, please follow other related articles on the PHP Chinese website!