Explore several methods to traverse two-dimensional arrays in php. Copy the code as follows :? Php uses for loop traversal $ arr2array (array (Zhang San, 20, men), array (Li Si, 25, men), array (Wang Wu, 19, female), array (Zhao Liu, 25, female); ec
The code is as follows:
// Use for loop traversal
$ Arr2 = array ("zhang san", "20", "male"), array ("Li Si", "25", "male "), array ("Wang Wu", "19", "female"), array ("Zhao Liu", "25", "female "));
Echo"
Name |
AgeGender |
";For ($ I = 0; $ I <4; $ I ++ ){Echo"
";For ($ j = 0; $ j <3; $ j ++ ){Echo"
"; Echo $ arr2 [$ I] [$ j]; Echo" | ";}Echo"
";Echo"";}Echo"
";
?>
// Use foreach for traversal
The code is as follows:
$ Arr = array ('one' => array ('name' => 'Zhang San', 'age' => '23', 'sex' => 'male '),
'Two' => array ('name' => 'Lily', 'age' => '43 ', 'sex' => 'female '),
'Three '=> array ('name' => '王', 'age' => '32', 'sex' => 'mal '),
'Four' => array ('name' => 'Zhao Liu', 'age' => '12', 'sex' => 'female '));
Foreach ($ arr as $ k => $ val ){
Echo $ val ['name']. $ val ['age']. $ val ['sex']."
";
}
Echo"
";
?>
The code is as follows:
$ Arr = array ('one' => array ('name' => 'Zhang San', 'age' => '23', 'sex' => 'male '),
'Two' => array ('name' => 'Lily', 'age' => '43 ', 'sex' => 'female '),
'Three '=> array ('name' => '王', 'age' => '32', 'sex' => 'mal '),
'Four' => array ('name' => 'Zhao Liu', 'age' => '12', 'sex' => 'female '));
Foreach ($ arr as $ key => $ value ){
Foreach ($ value as $ key2 => $ value2 ){
Echo $ value2;
}
Echo"
";
}
?>
The http://www.bkjia.com/PHPjc/327480.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327480.htmlTechArticle code is as follows :? Php // use for loop traversal $ arr2 = array ("zhang san", "20", "male"), array ("Li Si", "25 ", "Male"), array ("Wang Wu", "19", "female"), array ("Zhao Liu", "25", "female ")); ec...