如何使用PHP将多维关联数组的缺失值添加到HTML表中

如何使用PHP将多维关联数组的缺失值添加到HTML表中,php,html,arrays,Php,Html,Arrays,我试图用php将数组打印到html表,但遇到数组元素数量不同的问题(例如缺少字段) 我如何解决数组中有时缺少一个(或多个)头元素的问题,从而使值最终位于错误的头下 这是我的密码。我想我需要添加另一个循环,以确保所有的$行都与$键对齐 输入阵列: array (size=16) 0=> 'created_by' => string 'me@example.com' (length=31) 'bug_status' => string 'verified' (length

我试图用php将数组打印到html表,但遇到数组元素数量不同的问题(例如缺少字段)

我如何解决数组中有时缺少一个(或多个)头元素的问题,从而使值最终位于错误的头下

这是我的密码。我想我需要添加另一个循环,以确保所有的$行都与$键对齐

输入阵列:

array (size=16)
 0=>
  'created_by' => string 'me@example.com' (length=31)
  'bug_status' => string 'verified' (length=8)
  'reported_by' => string 'me@example.com' (length=31)
  'modified_ts' => string '1413503800000' (length=13)
  'bug_id' => string '123' (length=3)
  'bug_severity' => string 'normal' (length=6)
  'product' => string 'core graveyard' (length=14)
  'bug_version_num' => string '9' (length=1)
  'assigned_to' => string 'me@example.com' (length=19)
  'op_sys' => string 'windows nt' (length=10)
  '_id' => string '123.1217503800000' (length=17)
  'component' => string 'viewer app' (length=10)
  'modified_by' => string 'nobody@example.org' (length=18)
  'priority' => string 'p2' (length=2)
  'qa_contact' => string '#unknown' (length=8)
  'created_ts' => string '901720800000' (length=12)
以下是我的PHP代码:

$keys = array_keys($array[0]);
echo "<table><tr><th>".implode("</th><th>", $keys)."</th></tr>";
foreach ($array as $rows) {
  if (!is_array($rows))
    continue;
  echo "<tr><td>".implode("</td><td>", $rows )."</td></tr>";
}
echo "</table> 
$keys=array_keys($array[0]);
echo“”。内爆(“,$keys”);
foreach($array作为$rows){
如果(!is_数组($rows))
继续;
echo“”。内爆(“,$rows)。”;
}
回声“

这就是我要做的:

$keys = array_keys($array[0]);
echo '<table><tr><th>'.implode('</th><th>', $keys).'</th></tr>';

foreach ($array as $row){
    if (!is_array($row)) continue;

    //Go through each of the keys you need and set them to empty if they're not set
    foreach($keys as $keyName){
        if (!isset($row[$keyName])  $row[$keyName] = '';
    }

    echo '<tr><td>'.implode("</td><td>", $row ).'</td></tr>';
}
echo '</table>';
$keys=array_keys($array[0]);
回声“”。内爆(“”,$keys)。“”;
foreach($array作为$row){
如果(!is_数组($row))继续;
//检查您需要的每个键,如果没有设置,则将其设置为空
foreach($keys作为$keyName){
如果(!isset($row[$keyName])$row[$keyName]='';
}
回显“”。内爆(“,$row)。”;
}
回声';

或者,您可以使用
empty()
is_null()
,或者其他检查,而不是
!isset()
,这取决于您期望/测试的内容。

这将为您创建表和标题,然后仅在该键存在时插入值。我确信它可以改进,但在使用示例数据时效果良好

$array = array(
    array(
        'a' => '1',
        'b' => '2',
        'c' => '3'
    ),
    array(
        'a' => '1',
        'b' => '2',
        'd' => '4',
        'e' => '5'
    )
);

$headers = array();
$thead = "<thead>";
foreach($array as $innerArray) {
    foreach($innerArray as $key => $value) {
        if (!in_array($key, $headers)) {
            $thead .= "<th>" . $key . "</th>";
            $headers[] = $key;
        }
    }
}
$thead .= "</thead>";

$tbody = "<tbody>";
foreach($array as $innerArray) {
    $tbody .= "<tr>";
    foreach($headers as $th) {
        $tbody .= "<td>";
        if (isset($innerArray[$th])) {
            $tbody .= $innerArray[$th];
        }
        $tbody .= "</td>";
    }
    $tbody .= "</tr>";
}

$table = "<table>" . $thead . $tbody . "</table>";

echo $table;
$array=array(
排列(
“a”=>“1”,
“b”=>“2”,
“c”=>“3”
),
排列(
“a”=>“1”,
“b”=>“2”,
'd'=>'4',
“e”=>“5”
)
);
$headers=array();
$thead=“”;
foreach($array作为$innerArray){
foreach($innerArray作为$key=>$value){
if(!in_数组($key,$headers)){
$thead.=“.$key.”;
$headers[]=$key;
}
}
}
$thead.=”;
$tbody=“”;
foreach($array作为$innerArray){
$tbody.=”;
foreach($th作为标题){
$tbody.=”;
if(isset($innerArray[$th])){
$tbody.=$innerArray[$th];
}
$tbody.=”;
}
$tbody.=”;
}
$table=“”.$thead.$tbody。”;
echo$表;

我没有做-1,但是运行这个时会得到什么输出?输入数组是什么样子的?把这变成一个问题以避免向下投票。所以policy.it会输出HTML,但行中的值并不总是与标题对齐,因为该数组缺少元素。
array\u pad($input,16,”)
这看起来不错,只是一个问题,如果第一个数组不包含所有字段怎么办?有没有办法找到包含最多键的数组,并使用它生成标头和$keys数组?