Home  >  Article  >  Backend Development  >  How to use array_reverse function in php

How to use array_reverse function in php

藏色散人
藏色散人Original
2019-05-24 16:21:183515browse

Usage of array_reverse function in php: [array_reverse(array,preserve)]. This function can reverse the order of elements in the original array, create a new array and return it.

How to use array_reverse function in php

php The array_reverse function is used to return an array in the reverse order of elements. Its syntax is array_reverse(array,preserve). The parameter array is required and refers to the specified array.

(Recommended tutorial: php video tutorial)

How to use the php array_reverse function?

Function: Use the opposite element Returns the array in sequence

Syntax:

array_reverse(array,preserve)

Parameters:

array Required. Specifies an array.

preserve Optional. Specifies whether to retain the original array key names.

Instructions:

Reverse the order of elements in the original array, create a new array and return it. If the second parameter is specified as true, the element's key name remains unchanged, otherwise the key name is lost.

php array_reverse() function usage example 1

<?php
$a = array("class" => "php中文网","name" => "西门","job" => "讲师");
print_r(array_reverse($a));
?>

Output:

Array ( [job] => 讲师 [name] => 西门 [class] => php中文网 )

php array_reverse() function usage example 2

<?php
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));
?>

Output:

Array ( [c] => Toyota [b] => BMW [a] => Volvo )

The above is the detailed content of How to use array_reverse function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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