In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-10-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to reverse php array", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to reverse php array"!
Php array inversion implementation method: 1, use "function reverse($arr){...} "method implements array inversion;2. Use"function reverse_arr($arr){...} "The array can be inverted using the method.
Operating environment: Windows 7 system, PHP 7.4 version, Dell G3 computer.
Php implements array inversion
There is a function in php that can invert arrays, and it is often used in work. Today, I am going to perform this function myself.
$arr = [2,5,6,1,8,16,12];function reverse($arr){ $left = 0; $right = count($arr) -1; $temp = []; while ($left 12 [1] => 16 [2] => 8 [4] => 6 [5] => 5 [6] => 2)
However, this function can only handle one-dimensional arrays. In implementing a multi-dimensional.
$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 result 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)
Above are numerical index arrays, can not handle associative arrays, and then to a can handle associative arrays
$arr = ['a'=>'aa','b'=>'bb','c'=>'cc','d'=>'dd','e'=>'ee'];function reverse($arr){ $temp = []; end($arr); while (($value = current($arr)) != null){ $temp[key($arr)] = $value; prev($arr); } return $temp;}print_r(reverse($arr)); result Array( [e] => ee [d] => dd [c] => cc [b] => bb [a]=> aa) At this point, I believe that everyone has a deeper understanding of "how to invert php arrays", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.