Php traverses two-dimensional arrays and outputs them as tables.

Source: Internet
Author: User

Php traverses two-dimensional arrays and outputs them as tables.

I. Index Array

<? Php // use the array () statement structure to declare all data in the contact list as a two-dimensional array, the default subscript is A sequential numeric index $ contact1 = array (// defines the outer array (1, 'gao mou', 'a company', 'beijing', '(010) 987654321 ', 'GM @ Linux.com'), // sub-array 1 array (2, 'loom', 'B company', 'shanghai', '(021) 123456789 ', 'lm @ apache.com '), // sub-array 2 array (3, 'fengmou', 'c company', 'tianjin ',' (022) 24680246 ', 'fm @ mysql.com '), // sub-array 3 array (4, 'shumao', 'd company', 'chongqing', '(023) 100 ', 'sm @ php.com ') // sub Array 4 ); // output each element of the Two-dimensional array in the form of an HTML table echo '<table border = "1" width = "600" align = "center"> '; echo '<caption> 

Output result

2. Associate an array (for loop is not available)

$ Contact2 = array ("Beijing contact" => array (1, 'taobao', 'Company A', 'beijing', '(010) 987654321 ', 'GM @ linux.com '), "Shanghai contact" => array (2, 'rolim',' B company ', 'shanghai',' (021) 123456789 ', 'lm @ apache.com '), "Tianjin contact" => array (3, 'fengmou', 'c company', 'tianjin ',' (022) 246802468 ', 'fm @ mysql.com '), "Chongqing contact" => array (4, 'shu mou', 'd company', 'chongqing ',' (023) 135791357 ', 'sm @ php.com '); // create a table and input '< table border = "1" width = "600" align = "center">' in an array loop '; echo '<tr bgcolor = "# dddddd"> '; echo '<th> Number </th> <th> name </th> <th> company </th> <th> region </th> <th> phone number </th>/ th> <th> EMALL </th> '; echo '</tr>'; foreach ($ contact2 as $ key => $ value) {echo '<tr> '; // It is also possible to nest A for loop in foreach/* for ($ n = 0; $ n <count ($ value); $ n ++) {echo "<td> $ value [$ n] </td>";} * // nested foreach ($ value as $ mn) in foreach) {echo "<td >{$ mn} </td>";} echo '</tr>';} echo '</table> ';

Output result:

Note:1. The for loop cannot be used directly for correlated arrays.

2. When creating a table, the table, row, and column Code are in pairs. Do not miss it. Do not forget to add a slash/

3. Try to use double quotation marks when using double quotation marks.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.