Agnes Tom Agnes Tom - 5 months ago 8
PHP Question

Array formatting printing table

I have loop, something like:

foreach($cars as $car){
$html .= $car[name];
$html .= $car[color];
$html .= $car[doors];
}


It print it like a table:

Mercedes | red | 3
Mercedes | blue | 3
Mercedes | red | 5
Ford | green | 4
Ford | green | 5


How print it like:

Mercedes
red | 3
blue | 3
red | 5

Ford
green | 4
green | 5


Please give me some tips.

Answer

In the web I found this:

`foreach($features as $key => $feature){
{
$html .= '<table style=" margin:0 20px; border:0;">';
$_features = $feature['features'];
{
if(!isset($features[$key-1]) || $feature['groupname'] != $features[$key-1]     ['groupname'])
$html .= '<tr><td colspan="2">'.$feature['groupname'].'</td></tr>';
$html .= '<tr>';
$html .= '<td style="padding: 3px 5px 3px 0;width:230px;float:     left;background-color: #F4F4F6; font-family: Tahoma, Geneva, sans-serif; font-    size:16px; text-align: right;">'.$feature['name'].' </td>';
$html .= '<td style="padding: 3px 0px 3px 10px;width: 400px;float: left;background-color:#ECECEC; font-family: Tahoma, Geneva, sans-serif; font-size:16px; font-weight: bold;text-align: left;">'.$feature['value'].'</td>';
$html .= '</tr>';
}
$html .= '</table>';
}`

It helped me solve my problem.

Comments