Home > Article > Backend Development > A simple example of using foreach() to traverse a two-dimensional array in PHP, foreach two-dimensional array_PHP tutorial
The first type
Want to use foreach() to traverse the entire two-dimensional array:
$team = array('lk','ok'); $book = array('linux服务器配置与管理',$team); foreach($book as $k=>$val) //for $book each $value( as ) echo $k.'=>'.$val.'';
The output result is:
0=>linux server configuration and management
1=>Array
Of course, I actually want all the specific content, not the output array. . .
So you should adopt the following approach and add a judgment statement:
$team = array('lk','ok'); $book = array('linux服务器配置与管理',$team); foreach($book as $k=>$val) //意思是for $book each $value( as ) if( is_array($val) ) foreach( $val as $value) echo $value.''; else echo $k.'=>'.$val.'';
then the output is:
0=>linux server configuration and management
lk
ok
echo "<br>"; echo "<h1>php遍历二维数组</h1>"; //$team = array('lk','ok'); //$book = array('linux服务器配置与管理',$team); $arr = array( array('name'=>'系统配置','url'=>'?action=config&do=config'), array('name'=>'验证码配置','url'=>'?action=config&do=seccode'), array('name'=>'模板管理','url'=>'?action=config&do=tpl'), array('name'=>'帐号管理','url'=>'?action=admin&do=list'), array('name'=>'添加帐号','url'=>'?action=admin&do=add')); foreach($arr as $k=>$val){ echo "name:".$val["name"]."/n"; }
The above simple example of using foreach() to traverse a two-dimensional array in PHP is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Bangkejia.