PHP has a function to reverse the array, the work is also often used, very convenient. Today to implement such a function yourself.
$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;} Effect Array ( [0] = [ 1] = [ 2] = 8 [4] = 6 [5] = 5 [6] = 2)
However, this function can only handle one-dimensional arrays. In the implementation one can handle multidimensional.
$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)));
Output
Array( [0] = 0 [1] = 5 [2] = = Array ( [0] = = Array ( [0] = > 7 [1] = 8 [2] = [ 1] = 1 [2] = 2 [3] = 5 ) [3] = = 1 [4] = = Array ( [0] = 9 [1] = 3 [2] = 6 ) [5] = 2) /c30>
PHP implements array inversion