-
-
/** - * Traverse two-dimensional array
- * edit bbs.it-home.org
- */
- //for loop traversal
- $arr2=array(array("Zhang San","20","Male") ,array("李四","25","male"),array("王五","19","female"),array("Zhao Liu","25","female"));
- echo "
Name | Age | Gender< /tr>";
- for($i=0;$i<4;$i++){
- echo "
";
- for($j=0;$j<3;$j++){
- echo "
";
- echo $arr2[$i][$j];
- echo "
| ";
- }
- echo "
";
- echo "
" ;
- }
- echo "
";
- ?>
//foreach traversal
- $arr = array('one'=>array('name'=> ;'张三','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 "
";
//foreach traversal example 2
- $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 "
" ;
- }
- ?>
-
-
Copy code
|