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

How to reverse php array

藏色散人
Release: 2023-03-14 11:56:01
Original
4112 people have browsed it

How to implement array reversal in php: 1. Use the "function reverse($arr){...}" method to achieve array reversal; 2. Use "function reverse_arr($arr){... }" method can be used to reverse the array.

How to reverse php array

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

php implements array reversal

There is a function in php that can reverse an array, which is often used in work and is very convenient. Let’s implement this function by ourselves today.

$arr = [2,5,6,1,8,16,12];
function reverse($arr){
    $left = 0;
    $right = count($arr) -1;
    $temp = [];
    while ($left <= $right){
        $temp[$left] = $arr[$right];
        $temp[$right]  = $arr[$left];
        $left++;
        $right--;
    }
    ksort($temp);
    return $temp;
}

效果

Array
(
    [0] => 12
    [1] => 16
    [2] => 8
    [4] => 6
    [5] => 5
    [6] => 2
)
Copy after login

However, this function can only handle one-dimensional arrays. While implementing one that can handle multiple dimensions.

$arr = [2,[6,3,9],1,[5,2,1,[10,8,7]],5,0];

function reverse_arr($arr){
    $index = 0;
    $reverse_array = [];
    foreach ($arr as $sub_arr){
        if(is_array($sub_arr)){
            $sub_arr = reverse($sub_arr);
            $arr_ = reverse_arr($sub_arr);
            $reverse_array[$index] = $arr_;
        }else{
            $reverse_array[$index] = $sub_arr;
        }
        $index++;
    }
    return $reverse_array;
}

print_r(reverse(reverse_arr($arr)));

输出结果
Copy after login
Array
(
    [0] => 0
    [1] => 5
    [2] => Array
        (
            [0] => Array
                (
                    [0] => 7
                    [1] => 8
                    [2] => 10
                )

            [1] => 1
            [2] => 2
            [3] => 5
        )

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

    [5] => 2
)
Copy after login

The above are all numeric index arrays and cannot handle associative arrays. Next comes the one that can handle associative arrays

$arr = [&#39;a&#39;=>&#39;aa&#39;,&#39;b&#39;=>&#39;bb&#39;,&#39;c&#39;=>&#39;cc&#39;,&#39;d&#39;=>&#39;dd&#39;,&#39;e&#39;=>&#39;ee&#39;];
function reverse($arr){
    $temp = [];
    end($arr);
    while (($value = current($arr)) != null){
        $temp[key($arr)] = $value;
        prev($arr);
    }
    return $temp;
}
print_r(reverse($arr));

结果
Array
(
    [e] => ee
    [d] => dd
    [c] => cc
    [b] => bb
    [a] => aa
)
Copy after login

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to reverse php array. 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