Using foreach () in PHP to traverse a simple example of a two-dimensional array, the foreach two-dimensional array _ PHP Tutorial

Source: Internet
Author: User
In PHP, use foreach () to traverse a simple example of a two-dimensional array. PHP uses foreach () to traverse a simple example of a two-dimensional array. The first type of foreach two-dimensional array wants to traverse the whole two-dimensional array with foreach (): $ teamarray (lk, OK ); $ bookarray (using foreach () in lin PHP to traverse simple instances of two-dimensional arrays, foreach two-dimensional arrays

Type 1

I want to use foreach () to traverse the entire two-dimensional array:

$ Team = array ('LK ',' OK '); $ book = array ('Linux server configuration and management', $ team ); foreach ($ book as $ k => $ val) // for $ book each $ value (as) echo $ k. '=> '. $ val. '';

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...

Therefore, add a judgment statement as follows:

$ Team = array ('LK ',' OK '); $ book = array ('Linux server configuration and management', $ team ); foreach ($ book as $ k => $ val) // meaning for $ book each $ value (as) if (is_array ($ val )) foreach ($ val as $ value) echo $ value. ''; else echo $ k. '=> '. $ val. '';

The output is:

0 => linux server configuration and management
Lk
OK

Echo"
"; Echo" php traversing two-dimensional arrays "; // $ team = array ('LK ',' OK '); // $ book = array ('Linux server configuration and management', $ team); $ arr = array ('name' => 'system configuration ', 'URL' => '? Action = config & do = config'), array ('name' => 'verification code configuration', 'URL' => '? Action = config & do = seccode'), array ('name' => 'template management', 'URL' => '? Action = config & do = tpl '), array ('name' => 'account management', 'URL' => '? Action = admin & do = list'), array ('name' => 'Add account', 'URL' => '? Action = admin & do = add'); foreach ($ arr as $ k => $ val) {echo "name :". $ val ["name"]. "/n ";}

The simple example of traversing a two-dimensional array using foreach () in PHP above is all the content shared by Xiaobian. I hope you can give us a reference and support for the house.

A simple example of traversing a two-dimensional array. The first type of the foreach two-dimensional array is to traverse the whole two-dimensional array using foreach (): $ team = array ('LK ',' OK '); $ book = array ('Lin...

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.