Home > Backend Development > PHP Tutorial > PHP two-dimensional associative array traversal method

PHP two-dimensional associative array traversal method

一个新手
Release: 2023-03-16 17:16:01
Original
1894 people have browsed it

The foreach loop is used to traverse the two-dimensional index array, which is relatively faster and more efficient. The foreach loop is specially used to loop arrays in PHP.
The examples are relatively simple, just practice more and think clearly about the program running logic.


<?php
	$arr = array(//定义外层数组
    "北京负责人"=>array(1,&#39;高某&#39;,&#39;A公司&#39;,&#39;北京市&#39;,&#39;(010)987654321&#39;,&#39;gm@Linux.com&#39;),//子数组1
    "上海负责人"=>array(2,&#39;洛某&#39;,&#39;B公司&#39;,&#39;上海市&#39;,&#39;(021)123456789&#39;,&#39;lm@apache.com&#39;),//子数组2
    "天津负责人"=>array(3,&#39;峰某&#39;,&#39;C公司&#39;,&#39;天津市&#39;,&#39;(022)24680246&#39;,&#39;fm@mysql.com&#39;),  //子数组3
    "重庆负责人"=>array(4,&#39;书某&#39;,&#39;D公司&#39;,&#39;重庆市&#39;,&#39;(023)13579135&#39;,&#39;sm@php.com&#39;)     //子数组4
    );

	foreach($arr as $key=>$arr_item){
		echo $key;	//取出数组的键
		echo "<pre class="brush:php;toolbar:false">";
			print_r($arr_item);	//$arr_item就是子数组了
		echo "
"; foreach($arr_item as $value){ echo $value."==="; //数组的值 } } ?>
Copy after login

Summary:
1. When traversing a two-dimensional associative array, the for loop may not be used.
2. $key, take out the key of $arr array
3. $arr_item is the subarray (subarray 1, subarray 2...)

The above is the detailed content of PHP two-dimensional associative array traversal method. 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