How to reverse the order of an array in php

青灯夜游
Release: 2023-03-08 22:40:02
Original
3042 people have browsed it

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).

How to reverse the order of an array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php reverses the order of the array

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
$arr1 =array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
echo "原数组顺序:";
var_dump($arr1);
$arr2=array_reverse($arr1);
echo "<br>数组反转后的顺序:";
var_dump($arr2);
?>
Copy after login

Output:

原数组顺序:array (size=3)
  &#39;a&#39; => string &#39;Volvo&#39; (length=5)
  &#39;b&#39; => string &#39;BMW&#39; (length=3)
  &#39;c&#39; => string &#39;Toyota&#39; (length=6)

数组反转后的顺序:array (size=3)
  &#39;c&#39; => string &#39;Toyota&#39; (length=6)
  &#39;b&#39; => string &#39;BMW&#39; (length=3)
  &#39;a&#39; => string &#39;Volvo&#39; (length=5)
Copy after login

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)
Copy after login

How to reverse the order of an array in php

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template