How to use foreach () to traverse a two-dimensional array and a multidimensional array

Source: Internet
Author: User
The first type of

To traverse the entire two-dimensional array with foreach ():

$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 is:

0=>linux Server Configuration and management
1=>array

Of course, I actually want all the details, not the output array ...

Therefore, you should use the following procedure to add a judgment statement:

$team = Array (' lk ', ' OK '), $book = Array (' Linux server Configuration and Management ', $team); foreach ($book as $k + $val)//means 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 "<br>", echo "

The second type of

foreach () iterates over a multidimensional array:

<?php//declares a three-dimensional array $info =array ("User" =>array (Array (1, "Zhangsan", "Nan" "), Array (2," Lisi "," Nan "), Array (3," Wangwu ", +," NV ")," Score "=>array (Array (1,100,98,95,96), Array ( 2,56,98,87,84), Array (3,68,75,84,79)), "Connect" =>array (Array (1, ' 2468246 ', "salkh@bbs.com"), Array (2, ' 343681643 ', "aikdki@sina.com"), Array (3, ' 3618468 ', "42816@qq.com")); Loop traversal, output a table foreach ($info as $tableName + = $table) {echo "<table align= ' center ' border= ' 1 ' width=300>"; echo "& Lt;caption>

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.