php中的表格显示

这是我的数组:例如


Array(

[0]=>Name1

[1]=>Name2

)

Array(

[0]=>value1

[1]=>value2

)

Array(

[0]=>age1

[1]=>age2

)

我想以表格格式显示这个数组,所有第 0 个元素都应该在一行中,所有第 1 个元素都应该在另一行中,并且它应该按照 der 构建 3 个列是 3 个数组请帮我解决这个问题


我的结果应该是:


Col1            Col2             Col3

Name1         Value1.        Age1

Name2.        Value2.         Age2





Array ( [political] =>

      Array ( 

         [0] => Krishna Colony 

         [1] => Police Head Quarters) 

[locality] =>

      Array 

          ([0] => Chittorgarh 

           [1] => Ratnagiri ) 

[administrative_area_level_2] =>

       Array 

            ( [0] => Chittaurgarh 

              [1] => Ratnagiri ) 

[administrative_area_level_1] =>

        Array 

          ( [0] => Rajasthan

           [1] => Maharashtra ) 

[country] => 

         Array 

          ( [0] => India 

            [1] => India ) 

[street_number] =>

          Array 

           ( [0] => A.P.Mission Camp ) 

 ) 

我的原始数组是所有的键都应该显示在第一行的一列 [0] 和第二行的 [1] 我的问题是行是动态的


杨魅力
浏览 190回答 3
3回答

幕布斯6054654

编辑问题已更改试试这个,我所做的是使用 2 个循环来获取数组的标题。另一个循环是显示结果。$arr = [&nbsp; &nbsp; 'political'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> [&nbsp; &nbsp; &nbsp; &nbsp; 0 => 'Krishna Colony',&nbsp; &nbsp; &nbsp; &nbsp; 1 => 'Police Head Quarters'&nbsp; &nbsp; ],&nbsp; &nbsp; 'locality'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => [&nbsp; &nbsp; &nbsp; &nbsp; 0 => 'Chittorgarh ',&nbsp; &nbsp; &nbsp; &nbsp; 1 => 'Ratnagiri '&nbsp; &nbsp; ],&nbsp; &nbsp; 'administrative_area_level_2' => [&nbsp; &nbsp; &nbsp; &nbsp; 0 => 'Chittaurgarh ',&nbsp; &nbsp; &nbsp; &nbsp; 1 => 'Ratnagiri '],&nbsp; &nbsp; 'administrative_area_level_1' => [&nbsp; &nbsp; &nbsp; &nbsp; 0 => 'Rajasthan',&nbsp; &nbsp; &nbsp; &nbsp; 1 => 'Maharashtra '&nbsp; &nbsp; ],&nbsp; &nbsp; 'country'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> [&nbsp; &nbsp; &nbsp; &nbsp; 0 => 'Chittorgarh ',&nbsp; &nbsp; &nbsp; &nbsp; 1 => 'Ratnagiri '&nbsp; &nbsp; ],&nbsp; &nbsp; 'street_number'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> [&nbsp; &nbsp; &nbsp; &nbsp; 0 => 'India ',&nbsp; &nbsp; &nbsp; &nbsp; 1 => 'India '],];echo "<table id='table-loop' border='1'>";$parent = [];$total_count = 0;echo "<tr>";foreach ($arr as $a => $val) {&nbsp; &nbsp; echo "<th>$a</th>";&nbsp; array_push($parent, $a);&nbsp; $total_count = count($val);}echo "</tr>";$i = 0;foreach ($arr as $a) {&nbsp; if($i<$total_count){&nbsp; echo "<tr>";&nbsp; &nbsp; foreach($parent as $p){&nbsp; &nbsp; &nbsp; echo "<td>". $arr[$p][$i]. "</td>";&nbsp; &nbsp; }&nbsp; echo "</tr>";&nbsp; $i++;&nbsp; &nbsp; }}echo "</table>";结果将如下所示

泛舟湖上清波郎朗

<?php$a = [0 => 'Name1', 1 => 'Name2'];$b = [0 => 'value1', 1 => 'value2'];$c = [0 => 'age1', 1 => 'age2'];$length = count($a);echo '<table border="1">&nbsp;<th>Name</th><th>Value</th><th>Age</th>';for ($i = 0; $i <&nbsp; &nbsp; $length; $i++) {&nbsp; &nbsp; echo '<tr><td>' . $a[$i] . '</td><td> ' . $b[$i] . ' </td><td>' . $c[$i] . '</td></tr>';}echo '</table>';这些是有帮助的基本代码(当所有三个数组都具有相同的长度时)

倚天杖

假设数组的名称分别是 $name、$value 和 $age,并假设每个数组的编号相同。价值观;echo "<table><tr>";for($i=1; $i<4; $i++) {&nbsp; &nbsp; echo "<td>COL". $i . "</td>";}echo "</tr>";for($i=0; $i<2; $i++) {&nbsp; &nbsp; echo "<tr><td>". $name[$i] . "</td>";&nbsp; &nbsp; echo "<td>". $value[$i] . "</td>";&nbsp; &nbsp; echo "<td>". $age[$i] . "</td></tr>";}echo "</table>";希望这可以帮助;
打开App,查看更多内容
随时随地看视频慕课网APP