Exploring several methods for traversing two-dimensional arrays in php _ PHP Tutorial

Source: Internet
Author: User
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"











";For ($ I = 0; $ I <4; $ I ++ ){Echo" ";For ($ j = 0; $ j <3; $ j ++ ){Echo" ";}Echo" ";Echo"";}Echo"
Name AgeGender
";
Echo $ arr2 [$ I] [$ j];
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.