Home > Article > Backend Development > How to reverse the order of an array in php
You can use the array_reverse() function in PHP to reverse the order of the array. The syntax format is "array_reverse(array,preserve)"; the parameter preserve can be omitted and is used to specify whether to retain the key names of the original array (only For numeric key names, non-numeric keys are not affected).
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php reverses the order of the array
<?php header('content-type:text/html;charset=utf-8'); $arr1 =array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota"); echo "原数组顺序:"; var_dump($arr1); $arr2=array_reverse($arr1); echo "<br>数组反转后的顺序:"; var_dump($arr2); ?>
Output:
原数组顺序:array (size=3) 'a' => string 'Volvo' (length=5) 'b' => string 'BMW' (length=3) 'c' => string 'Toyota' (length=6) 数组反转后的顺序:array (size=3) 'c' => string 'Toyota' (length=6) 'b' => string 'BMW' (length=3) 'a' => string 'Volvo' (length=5)
Related function description:
array_reverse() function returns an array in reverse element order.
array_reverse() function reverses the order of elements in the original array, creates a new array and returns it. If the second argument is specified as true, the element's numeric key remains unchanged, otherwise the key is lost.
Grammar:
array_reverse(array,preserve)
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to reverse the order of an array in php. For more information, please follow other related articles on the PHP Chinese website!