How to reverse an array using array_reverse function in PHP

王林
Release: 2023-06-26 13:28:02
Original
1091 people have browsed it

array_reverse() is a commonly used function in PHP, which can be used to reverse the order of elements in an array. Here we will introduce how to reverse an array using array_reverse() function in PHP.

First of all, we need to clarify the syntax and parameters of the array_reverse() function.

Syntax: array array_reverse (array $array, bool $preserve_keys = false)

Parameters:

  1. array: required parameter, the array to be reversed .
  2. preserve_keys (optional parameter): The default is false. When true, the key names in the original array will be retained.

Sample Code:

Let's look at a simple sample code that will reverse a regular array.

// 创建一个常规数组
$fruits = array("apple","banana","orange","lemon");

// 反转数组
$reverse_fruits = array_reverse($fruits);

// 输出反转后的数组
print_r($reverse_fruits);
Copy after login

Output result:

Array
(
    [0] => lemon
    [1] => orange
    [2] => banana
    [3] => apple
)
Copy after login

In the above example, we defined a regular array $fruits containing four elements. Then we use the array_reverse() function to reverse the elements in the array. Finally, the reversed array is output through the print_r() function.

Next, let’s take a look at how to retain the key names in the original array.

// 创建一个关联数组
$fruits = array("a"=>"apple","b"=>"banana","c"=>"orange","d"=>"lemon");

// 反转数组, 保留键名
$reverse_fruits = array_reverse($fruits,true);

// 输出反转后的数组
print_r($reverse_fruits);
Copy after login

Output result:

Array
(
    [d] => lemon
    [c] => orange
    [b] => banana
    [a] => apple
)
Copy after login

In the above code, we define an associative array $fruits. We use the array_reverse() function to reverse the elements in the array and set the second parameter $preserve_keys to true. Finally, the reversed array is output through the print_r() function.

Summary:

The array_reverse() function is a commonly used function in PHP, which can help us easily reverse the order of elements in an array. When using the function, you need to pay attention to the parameters, especially the second parameter $preserve_keys. You need to choose whether to retain the key names in the original array as needed.

The above is the detailed content of How to reverse an array using array_reverse function 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!