In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to flexibly use foreach+list in PHP to deal with multi-dimension". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to flexibly use foreach+list to deal with multi-dimension in PHP".
Flexible use of foreach+list to deal with Multi-dimensional arrays in PHP
Throw the question first. Sometimes the parameters we receive are multidimensional arrays, and we need to convert them to normal arrays, such as:
1$ arr = [
2 [1, 2, [3, 4]]
3 [5, 6, [7, 8]]
4]
What we need is that element 1 becomes 1, 2, and element 2 becomes 5, 6, 7, and 8, respectively. At this point, we can implement it with foreach and list, and it's very simple:
1foreach ($arr as list ($a, $b, list ($c, $d)) {
2 echo $a,',', $b,',', $c,',', $d, PHP_EOL
3}
Isn't it very simple. Note, however, that list should specify the key name when disassembling the Hash array in the form of key-value pairs, and it can only be used in versions after 7.1.
1$ arr = [
2 ["a" = > 1, "b" = > 2]
3 ["a" = > 3, "b" = > 4]
4]
five
6foreach ($arr as list ("a" = > $a, "b" = > $b)) {
7 echo $a,',', $b, PHP_EOL
8}
nine
10foreach ($arr as ["a" = > $a, "b" = > $b]) {
11 echo $a,',', $b, PHP_EOL
12}
Note: if there is no key name, the output will be empty and will not report an error, this is a BUG point, be sure to pay attention.
The second way of writing in the above code is more simple and intuitive, which shows that we can also disassemble arrays in this way. And when you specify the key values, you don't have to care about their order:
1 ["b" = > $b, "a" = > $a] = $arr [0]
2echo $a,',', $b, PHP_EOL
It turns out that list () still has such grammatical candy, sure enough, we still have to keep learning, and the methods that have been used but have never been thoroughly understood can be of so much use. Say no more, and go on to study the other interesting things in the manual!
Test code: https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/source/%E5%9C%A8PHP%E4%B8%AD%E7%81%B5%E6%B4%BB%E4%BD%BF%E7%94%A8foreach%2Blist%E5%A4%84%E7%90%86%E5%A4%9A%E7%BB%B4%E6%95%B0%E7%BB%84.php
Thank you for reading, the above is the content of "how to flexibly use foreach+list to deal with multi-dimension in PHP". After the study of this article, I believe you have a deeper understanding of how to flexibly use foreach+list to deal with multi-dimension in PHP, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.