I am decoding a .json file with PHP. I have successfully put the json data into a html table.
The json data is in the form of: {..."id.type.subtype":value,"id.type.subtype2":value,"id.type1.subtype":value,...}
This is displayed in my HTML table as: <tr><td>id.type.subtype</td><td>value</td></tr>
I want to convert each unique id.type.subtype
into a unique human readable name like Thing
.
My full code:
<?php
$json = file_get_contents('file.json');
$jsonIterator = new
RecursiveIteratorIterator(new RecursiveArrayIterator(json_decode($json, TRUE)), RecursiveIteratorIterator::SELF_FIRST);
if (count($jsonIterator) > 0) {
echo "<table>
";
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
$cnt = count($val);
echo "<tr><td>$key</td></tr>
";
} else {
echo "<tr><td>$key</td><td>$val</td></tr>
";
}
}
echo "</table>
";
} else {
echo "<p>The iterator is empty.</p>";
}
?>
Placing the following just after the foreach()
is the only method I can think of. It works but would need an if/elseif for the 200-300 different keys
if ($key == 'id.type.subtype') {
$key = 'Thing';
}
I haven't used PHP for quite a long time. I understand that any method would require manually inputting each user friendly title, but there must be a more code efficient method of changing the keys. Is there another way?