A foreach loop is used to traverse a two-dimensional indexed array, which is relatively faster and more efficient, and the Foreach loop is specifically used in PHP for looping arrays.
Examples are relatively simple, more practice, to understand the program running logic can be.
<?php$arr = Array (//define outer array "Beijing Head" =>array (1, ' High ', ' a company ', ' Beijing ', ' (010) 987654321 ', ' [email protected] '),//Sub-array 1 "Shanghai Principal "=>array (2, ' Luo ', ' B Company ', ' Shanghai ', ' (021) 123456789 ', ' [email protected] '),//Sub-array 2" Tianjin Head "=>array (3, ' PK ', ' C Company ', ' Tianjin ', ' (022) 24680246 ', ' [email protected] '),//Sub-array 3 "Chongqing Head" =>array (4, ' Book a ', ' d company ', ' Chongqing ', ' (023) 13579135 ', ' [email Protected] ')//Sub-array 4), foreach ($arr as $key + $arr _item) {echo $key;//Remove the key of the array echo "<pre>";p rint_r ($arr _item); /$arr _item is the subarray of echo "</pre>"; foreach ($arr _item as $value) {echo $value. = = = ";//The value of the array}}?>
Summary:
1. When traversing a two-dimensional associative array, the FOR loop may not be used.
2, $key, remove the key of the $arr array
3, $arr _item is a sub-array (sub-array 1, sub-array 2 ...)
This article is from the "Golden Three" blog, please be sure to keep this source http://jinsanguo.blog.51cto.com/13363984/1973371
How to traverse the two-dimensional associative array of PHP