Discuss several methods of traversing two-dimensional arrays in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:08:24
Original
1204 people have browsed it

Copy code The code is as follows:

//Use a for loop to traverse
$arr2=array(array("Zhang San","20","Male"),array("李四","25","Male"),array("王五","19 ","Female"),array("Zhao Liu","25","Female"));
echo "";
for($i=0;$i<4;$i++){
echo "";
for($j=0;$j<3;$j++){
echo "";
}
echo "";
echo "
";
}
echo "
Name< /td>AgeGender
";
echo $arr2[$i ][$j];
echo "
";
?>

//Use foreach to traverse
Copy Code The code is as follows:

$arr = array('one'=>array('name'=>'Zhang San', 'age'=>'23','sex'=>'Male'),
'two'=>array('name'=>'李思','age'=>' 43','sex'=>'female'),
'three'=>array('name'=>'王五','age'=>'32','sex'= >'Male'),
'four'=>array('name'=>'Zhao Liu','age'=>'12','sex'=>'Female')) ;

foreach($arr as $k=>$val){
echo $val['name'].$val['age'].$val['sex']."
";
}
echo "

";
?>


Copy code Code As follows:

$arr = array('one'=>array('name'=>'Zhang San','age'=>'23 ','sex'=>'Male'),
'two'=>array('name'=>'李思','age'=>'43','sex'=> ;'Female'),
'three'=>array('name'=>'王五','age'=>'32','sex'=>'Male'),
'four'=>array('name'=>'Zhao Liu','age'=>'12','sex'=>'female'));
foreach($arr as $key=>$value){
foreach($value as $key2=>$value2){
echo $value2;
}
echo "
";
}

?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327480.htmlTechArticleCopy the code as follows: ?php //Use a for loop to traverse $arr2=array(array("Zhang San" ,"20","male"),array("李思","25","male"),array("王五","19","female"),array("Zhao Liu"," 25","female")); ec...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!