这是我的完整代码(工作)——感谢@poncha
<style type="text/css">
table{border-collapse: collapse;}
td{border:1px solid #aaa;}
.array{border-right:0px;}
</style>
<?php
$json_raw = '{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}';
function JSON_to_HTML($json){
echo "<table>";
foreach($json as $key=>$value){
if($value):
if(is_array($value)):
echo "<tr><td class='array'>{$key}:</td><td>";
JSON_to_HTML($value);
echo "</td></tr>";
else:
if(preg_match('/.png|.gif|.jpg.|jpeg/', $value)):
echo "<tr><td>{$key}:</td><td class='italic'>{$value}</td></tr>";
else:
echo "<tr><td>{$key}:</td><td>{$value}</td></tr>";
endif;
endif;
else:
echo "<tr><td class='field-grey'>{$key}:</td><td class='field-empty'><span class='null'>null</span></td></tr>";
endif;
}
echo "</table>";
}
echo "<table class='outer'>";
$data = json_decode($json_raw, true);
foreach($data as $key=>$value){
if($value):
if(is_array($value)):
echo "<tr><td class='array'>{$key}:</td><td>";
JSON_to_HTML($value);
echo "</td></tr>";
else: echo "<tr><td>{$key}:</td><td>{$value}</td></tr>";
endif;
else: echo "<tr><td>{$key}:</td><td class='field-empty'><span class='null'>null</span></td></tr>"; endif;
}
echo "</table>";
?>