基于数组中呈现的数据的HTML表

时间:2018-04-14 09:50:04

标签: php html arrays html-table

我需要根据存储在数组中的数据制作HTML表。

首先,我有嵌套数组,如下所示:

$cars=Array(

Array("DDT","Detroit","USA",

    Array(
                Array("DDT-55555","1x1","100Hp"),
                Array("DDT-66666","2x2","200Hp"),
                Array("DDT-77777","3x3","300Hp"),
                Array("DDT-77777","4x4","400Hp")
    )),


Array( "AMM","London","UK",

    Array(
                Array("AMM-888","6x6","500Hp")
    )),

);

$countries=Array();

然后我有一个带有选择标记的表单,用于选择命名的国家/地区:

echo "<form method='post' action='***.php'>

<select name='country' onchange='this.form.submit()'>";
foreach($countries as $country){
        echo "<option>".$country;
}   


echo "</select></form>";

我需要的是一个HTML表格,它根据我从选择表单中选择的内容生成来自cars-array的数据,如下所示:

<thead>
   <tr>
     <th>Mark</th>
     <th>City</th>
     <th>Country</th>
     <th>Model</th>
     <th>Numbers</th>
     <th>Horsepower</th>
   </tr>
</thead>

无法使其正常工作..

1 个答案:

答案 0 :(得分:0)

您需要检查每个数组的循环

$table= array();
foreach($cars as $currCar){    
    if($currCar[$selectedValue]){
      $table = key($currCar[$selectedValue])+1;
    }    
}
if(count($table)){
echo "<tbody>";
foreach($table as $currRow){
echo "<tr>";
    foreach($currRow as $currTd){
      echo "<td>";
        $currTd;
      echo "</td>";
    }
   echo "</tr>";
}
 echo "</tbody>";
}