No need to json encode the result, however with making tables I like to make a json template file to dictate how the table should be constructed so that I can make changes easily.
// this would normally be located elsewhere.
$templateJson = '"columns": {
"title":{
"title": "Title",
"class": "title"
},
"descr":{
"title": "Description",
"class": "descr"
}
}';
$template = json_decode($templateJson);
if(is_array($details))
{
$markup = "<table><thead><tr>";
foreach($template["columns"] as $col=>$format)
{
$markup .= '<th class="'.$format['class'].'">';
$markup .= $format['title'];
$markup .= '</th>';
}
$markup .= "</tr></thead><tbody>";
foreach($details as $i=>$row)
{
$markup .= '<tr>';
foreach($template as $col=>$format)
{
$markup .= '<td>'.$row[$col].'</td>';
}
$markup .= '</tr>';
}
$markup .= "</tbody></table>";
}
echo $markup;
I tend to do way more loops than I probably should though, there may be a more optimal way but this will give you valid table markup.