Home > Backend Development > PHP Problem > How to reverse an array in php

How to reverse an array in php

PHPz
Release: 2023-04-12 09:58:58
Original
850 people have browsed it

In the PHP programming language, array is a commonly used data type. When performing array operations, we sometimes need to reverse the order of elements in the array. We can use PHP built-in functions to achieve this purpose. This article will introduce how to use PHP to reverse an array. The functions involved include array_reverse() and array_flip().

  1. array_reverse() function

array_reverse() function is a built-in function in PHP for reversing an array. The syntax is as follows:

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

Parameter description:

$array: indicates to reverse The reversed array;

$preserve_keys: If the value of this parameter is true, the key names of the original array will be retained in the reversed array. The default is false.

Example demonstration:

$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
Copy after login
  1. array_flip() function

array_flip() function is used in PHP Built-in function for exchanging keys and values ​​in an array. If the keys and values ​​in the array are not repeated, you can use this function to easily interchange the keys and values ​​in the array. The operation of reversing an array essentially swaps the index value of the array with its corresponding value. Therefore, using the array_flip() function can also achieve the effect of array reversal.

The syntax of the array_flip() function is as follows:

array array_flip (array $array)

Parameter description:

$array : Indicates the array to be reversed.

Example demonstration:

$fruits = array(
    "apple",
    "banana",
    "orange",
    "lemon"
);
$flip_fruits = array_flip($fruits);
print_r($flip_fruits);
Copy after login

Output result:

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

As mentioned above, when using the array_flip() function to reverse the array, the keys and values ​​of the array will be exchanged. Convenient for array reversal operations. However, we can solve this problem through eccentric techniques. You can first use the array_flip() function to interchange the keys and values ​​of the array, then reverse the array, and then use the array_flip() function to restore the original correspondence between keys and values.

Sample code:

$fruits = array(
    "apple",
    "banana",
    "orange",
    "lemon"
);
$flip_fruits = array_flip($fruits);
$reverse_flip_fruits = array_reverse($flip_fruits, true);
$reverse_fruits = array_flip($reverse_flip_fruits);
print_r($reverse_fruits);
Copy after login

The output result is the same as the array_reverse() function sample code, which is:

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

The above are two methods of reversing an array in PHP: array_reverse () and array_flip() functions. It should be noted that the array reversed using these functions is reversed based on the order of the index values ​​of the original array. If the order of the original array has been disrupted, the reversed result will also be affected.

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

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