I have an array with following format:
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
)
)
I have to print the data in the table so that each key of array is printed as column header and the value as column data. That means Push to Web, attribute_set etc will be header and Yes, laminate_flooring will be data respectively.
I wrote the following but its not working.
$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);
Please help me to sort out the problem. Thanks in advance
array_keys()andarray_values()will help. php.net/manual/en/function.array-keys & php.net/manual/en/function.array-values.php Also use the alternate notation offoreach()when using inside HTML. It will look much cleaner.<?php foreach($array as $element): #your code endforeach; ?>