我有一个具有以下格式的数组:
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”,“ attribute_set等”将是标题,是的,“ ladder_flooring”将分别是数据。
我写了以下内容,但不起作用。
$table = '<table border="1" id="datatable">
<tr>';
foreach($data as $key=>$value){
$table .= '<td>'.$key.'</td>';
}
$table .= '</tr>';
foreach($data as $value){
$table .= '<tr>';
foreach($value as $innerkey=>$innervalue){
$table .= '<td>'.$innervalue.'</td>';
}
$table .= '</tr>';
}
}
}
$table .= '</table>';
print_r($table);
[请帮助我解决问题。在此先感谢
foreach($data as $key=>$value){
$table .= '<td>'.$key.'</td>';
}
这是因为您的顶级键是:
0 1 2 3..
在下一级中,您具有以下键:
Push to Web Category Name etc..
在此代码段中,您添加了innervalue
-数据(推到Web值,类别值,名称-值等等...] ...
...这些值与所需键处于同一级别。因此,$innerkey
是要添加到header:
中的键。foreach($data as $value){ $table .= '<tr>'; foreach($value as $innerkey=>$innervalue){ $table .= '<td>'.$innervalue.'</td>'; } $table .= '</tr>'; }
基本上就是这样:
$table = '<table border="1" id="datatable"> <tr>'; foreach($data as $key=>$value){ foreach($value as $innerkey=>$innervalue){ $table .= '<td>'.$key.'</td>'; } } $table .= '</tr>'; foreach($data as $value){ $table .= '<tr>'; foreach($value as $innerkey=>$innervalue){ $table .= '<td>'.$innervalue.'</td>'; } $table .= '</tr>'; } $table .= '</table>';