I am provided with a JSON file that I need to parse. I do some work for the hotel, and the ultimate goal is to create a table that lists:
Room Number
Adult Content (enabled or disabled)
Room Charges (enabled or disabled)
Status (occupied or unoccupied)
In the JSON file, I do not control. I was given, and from there I need to create the layout noted above.
This is an example of the JSON file I am giving (this is only part of the file, but you will understand how it works from this example):
{
"apiVersion" : "0.1",
"data" : {
"roomCount" : 105,
"rooms" : [
{
"room_number" : "104",
"services" : [
{
"adult" : {
"enabled" : true
},
"room_charges" : {
"enabled" : true
}
}
],
"status" : "OCCUPIED"
},
{
"room_number" : "105",
"services" : [
{
"adult" : {
"enabled" : true
},
"room_charges" : {
"enabled" : false
}
}
],
"status" : "OCCUPIED"
},
{
"room_number" : "106",
"services" : [
{
"adult" : {
"enabled" : false
},
"room_charges" : {
"enabled" : true
}
}
],
"status" : "OCCUPIED"
},
{
"room_number" : "107",
"services" : [
{
"adult" : {
"enabled" : false
},
"room_charges" : {
"enabled" : false
}
}
],
"status" : "OCCUPIED"
What i have done so far:
I tried to parse the data, and I can get the data to display, however, I am having problems installing it exactly the way I need to look like the one above. Currently, my script syntax prints this:
data
roomCount: 105
rooms
0
room_number: 104
services
0
adult
enabled: 1
room_charges
enabled: 1
status: OCCUPIED
, "". , , :
Room Number: 104
Adult: Enabled or Disabled (depending on true or false)
Room Charges: Enabled or Disabled (depending on true or false)
Status: OCCUPIED or UNOCCUPIED
, , , :
<?php
$string = file_get_contents("test.json");
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($string, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
echo "<br> $key";
} else {
echo "<br> $key: $val <br>";
}
}
?>
. - /, .