How to reverse array values ​​in php

藏色散人
Release: 2023-03-05 16:16:02
Original
3988 people have browsed it

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.

How to reverse array values ​​in php

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

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

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!