I am displaying the data from a json file onto a website, with recursive and iterative tables side by side. i have the iterative code working, but I have no idea on how to make it recursive.
Here is the code:
$dataArray = json_decode($data, true);
if(is_array($dataArray)){ //first level
$result = "<table class='arrayTable'>";
foreach($dataArray as $key=>$val){ //second level
$result.="<tr><td class ='key'>".$key.": </td><td class ='value'>";
if(gettype($val)=="array"){
$result .= "<table class='arrayTable'>";
foreach($val as $subkey=>$subval){ //third level
$result.="<tr><td class ='key2'>".$subkey.": </td><td class ='value2'>";
if(gettype($subval)=="array"){
$result .= "<table class='arrayTable'>";
foreach($subval as $lowkey=>$lowval){ //fourth level
$result.="<tr><td class ='key3'>".$lowkey.": </td><td class ='value3'>";
if(gettype($lowval)!="array"){
$result.=$lowval."</td></tr>";
}else{
$result.="</td></tr>";
}
}
$result.="</table>";
}else{
$result.=$subval."</td></tr>";
}
}
$result.="</table>";
}else{
$result.=$val."</td></tr>";
}
}
$result.= "</table>";
}else{
echo "Not a valid format!";
}
return $result;