Home  >  Article  >  Backend Development  >  php array reversal method

php array reversal method

PHPz
PHPzOriginal
2023-05-11 10:55:062867browse

This article will introduce the method of php array reversal.

In PHP, array is one of the most common data types, which can store multiple values. Implementing the operation of reversing an array is one of the common requirements. The following demonstrates several different methods to implement the php array reversal operation.

Method 1: array_reverse() function

PHP provides a built-in function array_reverse() to reverse the array. This function returns a new array in which the elements of the original array are in reverse order. arrangement. For example:

$arr = array(1, 2, 3, 4, 5);
$reversedArr = array_reverse($arr);
print_r($reversedArr);

The output result of running the above code is:

Array
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

This function can also choose to retain or reset the key name of the array. The second parameter is optional and its default value is false, which means the key name is not retained. If the value is true, the key name is preserved.

$arr = array("one" => 1, "two" => 2, "three" => 3, "four" => 4, "five" => 5);
$reversedArr = array_reverse($arr, true);
print_r($reversedArr);

The output result is:

Array
(
    [five] => 5
    [four] => 4
    [three] => 3
    [two] => 2
    [one] => 1
)

Method 2: for loop

The array reversal operation can also be implemented using the for loop. We can use two pointers, one at the beginning and one at the end of the array, move the two pointers in a loop and swap elements accordingly. The specific implementation is as follows:

$arr = array(1, 2, 3, 4, 5);
$len = count($arr);
for ($i = 0; $i < $len / 2; $i++) {
    $temp = $arr[$i];
    $arr[$i] = $arr[$len - $i - 1];
    $arr[$len - $i - 1] = $temp;
}
print_r($arr);

The output result is:

Array
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

Method 3: Use array_reduce() function and reverse array key name

We can also use array_reduce() function to Reverse the array. The array_reduce() function accepts an iterable array and can use callback functions to perform operations such as summing, filtering, and transformation on each element. The following is an example of array reversal:

$arr = array(1, 2, 3, 4, 5);
$reversedArr = array_reduce(array_keys($arr), function ($acc, $key) use ($arr) {
    $acc[$key] = $arr[count($arr) - 1 - $key];
    return $acc;
}, []);
print_r($reversedArr);

The output result is:

Array
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

The above code uses the array_keys() function to get the key name of the array and takes it as the first parameter Passed to the array_reduce() function for use in the callback function implementation. The next callback function uses the key name to calculate the reverse index of the array, thereby reversing the array. The parameter $acc is an accumulator whose value is always an array, with the initial value being the empty array [].

It can be seen that using the array_reduce() function to achieve array reversal is somewhat complicated. But this function is very useful when dealing with other array operations.

Method 4: Use the array_multisort() and array_column() functions

The last way to implement the array reversal operation is to use the features of PHP to handle multi-dimensional arrays. We can first use the array_multisort() function to sort the independent odd and even indexes in reverse order according to the index of the array. Then, use the array_column() function to convert the array back into a single array. The specific implementation is as follows:

$arr = array(1, 2, 3, 4, 5);
$n = count($arr);
for ($i = 0; $i < $n; $i++) {
    if ($i % 2 == 0) {
        $even[] = $arr[$i];
    } else {
        $odd[] = $arr[$i];
    }
}
array_multisort(array_reverse($odd), SORT_NUMERIC, array_reverse($even), SORT_NUMERIC);
$reversedArr = array_column(array($even, $odd), 0);
print_r($reversedArr);

The output result is:

Array
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

The above code stores the odd index and the even index in two arrays $odd and $even respectively. Next, use the array_multisort() function to sort the two arrays in reverse order, and finally use the array_column() function to convert the two-dimensional array into a single array.

Summary

This article discussed four different ways to implement PHP array reversal operations. Among them, the array_reverse() function is the simplest and most direct method. The for loop method is relatively simple to operate, but not as efficient as the array_reverse() function. Implementing array reversal using the array_reduce() function is relatively complex, but the function is very useful when working with other array operations. Finally, we show an approach based on the array_multisort() and array_column() functions to implement array reversal operations by taking advantage of PHP's capabilities for handling multidimensional arrays.

The above is the detailed content of php array reversal method. 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