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.