Home > Backend Development > PHP Tutorial > A simple example of using foreach() to traverse a two-dimensional array in PHP, foreach two-dimensional array_PHP tutorial

A simple example of using foreach() to traverse a two-dimensional array in PHP, foreach two-dimensional array_PHP tutorial

WBOY
Release: 2016-07-12 08:49:36
Original
1127 people have browsed it

A simple example of using foreach() in PHP to traverse a two-dimensional array, foreach two-dimensional array

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.''; 
Copy after login

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.''; 
Copy after login

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'=>'&#63;action=config&do=config'), 
array('name'=>'验证码配置','url'=>'&#63;action=config&do=seccode'), 
array('name'=>'模板管理','url'=>'&#63;action=config&do=tpl'), 
array('name'=>'帐号管理','url'=>'&#63;action=admin&do=list'), 
array('name'=>'添加帐号','url'=>'&#63;action=admin&do=add')); 
foreach($arr as $k=>$val){ 
  echo "name:".$val["name"]."/n"; 
} 
Copy after login

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1136652.htmlTechArticleA simple example of using foreach() to traverse a two-dimensional array in PHP. The first type of foreach two-dimensional array you want to use foreach() traverses the entire two-dimensional array: $team = array('lk','ok'); $book = array('lin...
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