4

I have been given a JSON file that I need to parse. I am doing some work for a hotel, and the end goal is to build a table that lists:

Room Number
Adult Content (enabled or disabled)
Room Charges (enabled or disabled)
Status (occupied or unoccupied)

The JSON file, I have no control over. It is given to me, and from there I have to create the layout noted above.

This is an example of the JSON file I am given (it is only part of the file, but you will understand how it works from this sample):

{
   "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 thus far:

I have tried to parse the data, and I can get the data to display, however, I am having trouble laying it out exactly the way I need it to look as stated above. Currently, my parse script outputs this:

data
roomCount: 105 

rooms
0
room_number: 104 

services
0
adult
enabled: 1 

room_charges
enabled: 1 

status: OCCUPIED 

I need it to NOT display the key for the nested array, "Services." What I would like as I said above is for the output to look like this:

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

And finally, here is the code that I have completed so far:

<?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>";
    }
}

?>

I am looking for some refinement on outputting the data in a cleaner way. If you can help or give me any suggestions/advice I will greatly appreciate it.

3
  • What is a size of a file. Commented Sep 30, 2014 at 23:54
  • so whats the problem here? Commented Sep 30, 2014 at 23:58
  • The file is 1478 lines of code. 37kb. I am looking for help on the output. I need it to mirror what I have explained in my question, in a clean and neat user-friendly way. Commented Oct 1, 2014 at 0:00

3 Answers 3

2

Here is json decoded as Object

$arr = json_decode($jsonString);
$out = "";
foreach ($arr->data->rooms as $k => $v) {
    $adult = $v->services[0]->adult->enabled == 'true' ? 'Enabled' : 'Disabled';
    $room = $v->services[0]->room_charges->enabled == 'true' ? 'Enabled' : 'Disabled';
    $out .="Room Number: $v->room_number\n";

    $out .="Adult: $adult\n";
    $out .="Room Charges: $room\n";
    $out .="Status: $v->status\n";
    $out .= "----\n";
}
echo $out;

Tested and here is an output

Room Number: 104
Adult: Enabled
Room Charges: Enabled
Status: OCCUPIED
----
Room Number: 105
Adult: Enabled
Room Charges: Disabled
Status: OCCUPIED
----
Room Number: 106
Adult: Disabled
Room Charges: Enabled
Status: OCCUPIED
----
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is exactly what I needed and the output is perfect.
1

Basically you just need to check if the keys and values are set:

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key=>$value) {
    if($key === 'room_number') {
        echo "Room Number: " . $value . "<br />";
    }
    if($key === 'adult') {
        echo "Adult: " . $value['enabled'] . "<br />";
    }
    if($key === 'status') {
        echo "status: " . $value . "<br />";
    }
}

Comments

1

For illustrational purposes you could just use a table or a description list on this case. The UI is up to you:

<?php

$response = '{ "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" } ] }}';
$data = json_decode($response, true);

?>

<style type="text/css">/*DL, DT, DD TAGS LIST DATA*/dl { margin-bottom:50px;} dl dt { background:#5f9be3; color:#fff; float:left; font-weight:bold; margin-right:10px; padding:5px; width:150px; } dl dd { margin:2px 0; padding:5px 0;}</style>
<dl>
<?php foreach($data['data']['rooms'] as $info): ?>
    <dt>Room Number: </dt>
    <dd><?php echo $info['room_number']; ?></dd>
    <?php $services = reset($info['services']); ?>
    <dt>Adult: </dt>
    <dd><?php echo ($services['adult']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?></dd>
    <dt>Room Charges: </dt>
    <dd><?php echo ($services['room_charges']['enabled'] == 1) ? 'ENABLED' : 'DISABLED'; ?></dd>
    <dt>Status: </dt>
    <dd><?php echo $info['status']; ?></dd><br/>
<?php endforeach; ?>
</dl>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.