Php 从具有正确标题行的数组打印表

Php 从具有正确标题行的数组打印表,php,arrays,Php,Arrays,我有一个具有以下格式的数组: Array ( [0] => Array ( [Push to Web] => Yes [attribute_set] => laminate_flooring [category] => Accessories/Underlay [name] => Under Pad Feather Light Foam

我有一个具有以下格式的数组:

Array
(
    [0] => Array
        (
            [Push to Web] => Yes
            [attribute_set] => laminate_flooring
            [category] => Accessories/Underlay
            [name] => Under Pad Feather Light Foam
            [sku] => 123-1028
            [description] => Floor Underlayment Feather Light (Vapour Barrier) 200 Sqft/Roll
            [short_description] => Floor Underlayment Feather Light (Vapour Barrier) 200 Sqft/Roll
            [image] => 123-1028.jpg
            [gallery_Image] => 9095307460638-424-424.jpg
            [price] => 0.24
            [qty_ca] => 16
            [weight] => 3
            [meta_description] => 51-1001
            [meta_title] => Under Pad Feather Light Foam
            [status] => 1
            [flooring_coverage] => 200
            [is_flooring_product] => 1
            [web_price_comparative] => 0.31
        )

    [1] => Array
        (
            [Push to Web] => Yes
            [category] => Accessories
            [name] => Vent Maple Flush 4x10
            [sku] => 089-1000
            [description] => Vent Flush Mount Maple 4 x 10
            [short_description] => Vent Flush Mount Maple 4 x 10
            [image] => 089-1000.jpg
            [price] => 17.05
            [qty_ca] => 63
            [qty_us] => 41
            [meta_description] => 10-1023
            [meta_title] => Vent Maple Flush 4x10
            [status] => 1
            [flooring_coverage] => 1
            [min_order_qty] => 400
            [web_price_comparative] => 22.16
        )
)
我必须打印表中的数据,以便将数组的每个键打印为列标题,将值打印为列数据。这意味着推送到Web、属性集等将是标题,是的,强化木地板将分别是数据

我写了以下内容,但它不起作用

 $row = 0;
$table  = '<table border="1" id="datatable">';
foreach($data as $value){
    $table .= '<tr>';
    if ($row == 0) {
        foreach($value as $innerkey=>$innervalue){
            $table .= '<td>'.$innerkey.'</td>';
        }    
    }
    $table .= '</tr><tr>';
    foreach($value as $innerkey=>$innervalue){
        $table .= '<td>'.$innervalue.'</td>';
    }
    $table .= '</tr>';
    $row++;
}
$table .= '</table>';

 print_r($table);
$row=0;
$table='';
foreach(数据为$value){
$table.='';
如果($row==0){
foreach($innerkey=>$innervalue的值){
$table.=''.$innerkey';
}    
}
$table.='';
foreach($innerkey=>$innervalue的值){
$table.=''.$innervalue'.';
}
$table.='';
$row++;
}
$table.='';
打印(表格);
该表的输出应如下所示:

但它的表现是这样的

第一个问题是标题行正在继续打印第一个数据行中的可用数据。其次,如果任何数据单元不可用,则由下一个单元数据填充。但它应该是银行的牢房。
请帮我解决这个问题。提前感谢

这假设所有子数组都使用相同的键,从图片上看,它们似乎使用相同的键

另外,我添加了htmlentities只是为了阻止任何恶意html破坏表,但是如果您知道不需要它,您可以删除它

    $table  = '<table border="1" id="datatable"><tr>';

    // Get the headers from the first child array.
    $headers = array_keys($data[0]);
    foreach($headers as $header){
        $table .= '<th>'.htmlentities($header).'</th>';
    }

    foreach($data as $value){

        $table .= '</tr><tr>';
        foreach($value as $innerkey=>$innervalue){
            $table .= '<td>'.htmlentities($innervalue).'</td>';
        }
        $table .= '</tr>';
    }
    $table .= '</table>';
$table='';
//从第一个子数组中获取头。
$headers=数组_键($data[0]);
foreach($headers作为$header){
$table.=''.htmlentities($header)。'';
}
foreach(数据为$value){
$table.='';
foreach($innerkey=>$innervalue的值){
$table.=''.htmlentities($innervalue)。'';
}
$table.='';
}
$table.='';

这假设所有子数组都使用相同的键,从图中可以看出它们是这样做的

另外,我添加了htmlentities只是为了阻止任何恶意html破坏表,但是如果您知道不需要它,您可以删除它

    $table  = '<table border="1" id="datatable"><tr>';

    // Get the headers from the first child array.
    $headers = array_keys($data[0]);
    foreach($headers as $header){
        $table .= '<th>'.htmlentities($header).'</th>';
    }

    foreach($data as $value){

        $table .= '</tr><tr>';
        foreach($value as $innerkey=>$innervalue){
            $table .= '<td>'.htmlentities($innervalue).'</td>';
        }
        $table .= '</tr>';
    }
    $table .= '</table>';
$table='';
//从第一个子数组中获取头。
$headers=数组_键($data[0]);
foreach($headers作为$header){
$table.=''.htmlentities($header)。'';
}
foreach(数据为$value){
$table.='';
foreach($innerkey=>$innervalue的值){
$table.=''.htmlentities($innervalue)。'';
}
$table.='';
}
$table.='';

所有子数组是否共享您稍后将用作表头的相同键?上面的示例数组具有不同的键。您可能希望规范化它们,即在导出源数据时包含空值。否则它会更复杂一些(你必须通过聚集所有数组成员的密钥来破译密钥的总列表)。那好了。您可以通过规范化标头并用空数据填充其他数组中缺少的标头来获得答案:)所有子数组是否共享稍后将用作表中标头的相同键?上面的示例数组具有不同的键。您可能希望规范化它们,即在导出源数据时包含空值。否则它会更复杂一些(你必须通过聚集所有数组成员的密钥来破译密钥的总列表)。那好了。您的答案是规范化标题并用空数据填充其他数组中缺少的标题:)mm。否,并非所有数组子元素中都存在标题行(数组键)。我用过你的代码,但还是一样。你可以在这里检查,这会让事情变得更难,我会玩一玩,如果我的答案有效的话,我会更新我的答案。否,并非所有数组子元素中都存在标题行(数组键)。我用过你的代码,但还是一样。你可以在这里检查,这会让事情变得更难,我会玩一玩,如果我的答案有效的话,我会更新我的答案。