Skip to content
Advertisement

How do i display the NESTED JSON data using php?

I coding a whole day just to display the nested Data from JSON that i got from the Api Provider but nothings gonna change. It’s always error! what should i do? Here’s my code hope you help me in this problem.

Imagine my name of json file is 05-03-21.json. and here’s the JSON Data

    {
   "result":"OK",
   "data":{
      "body":{
         "header":"Manila, National Capital Region, Philippines",
         "query":{
            "destination":{
               "id":"1004070",
               "value":"Manila, National Capital Region, Philippines",
               "resolvedLocation":"CITY:1004070:UNKNOWN:UNKNOWN"
            }
         },
         "searchResults":{
            "totalCount":1286,
            "results":[
               {
                  "id":438619,
                  "name":"OYO 653 Rosas Garden Hotel",
                  "starRating":3,
                  "urls":[
                     
                  ],
                  "address":{
                     "streetAddress":"1140 MH Del Pilar St., Ermita",
                     "extendedAddress":"",
                     "locality":"Manila",
                     "postalCode":"1000",
                     "region":"Manila",
                     "countryName":"Philippines",
                     "countryCode":"ph",
                     "obfuscate":false
                  },
                  "welcomeRewards":{
                     "collect":true
                  },
                  "guestReviews":{
                     "unformattedRating":7,
                     "rating":"7.0",
                     "total":29,
                     "scale":10,
                     "badge":"good",
                     "badgeText":"Good"
                  },
                  "landmarks":[
                     {
                        "label":"City centre",
                        "distance":"2.0 km"
                     },
                     {
                        "label":"Manila Bay",
                        "distance":"7.6 km"
                     }
                  ],
                  "geoBullets":[
                     
                  ],
                  "ratePlan":{
                     "price":{
                        "current":"P1,287",
                        "exactCurrent":1286.71,
                        "old":"P2,383",
                        "summary":"excluding taxes & fees"
                     },
                     "features":{
                        "freeCancellation":false,
                        "paymentPreference":false,
                        "noCCRequired":false
                     },
                     "type":"EC"
                  },
                  "neighbourhood":"Ermita",
                  "deals":{
                     "specialDeal":{
                        "dealText":"Save 46%"
                     },
                     "priceReasoning":"DRR-446"
                  },
                  "messaging":[
                     
                  ],
                  "badging":[
                     
                  ],
                  "pimmsAttributes":"DoubleStamps|D13|TESCO",
                  "coordinate":{
                     "lat":14.578154,
                     "lon":120.979947
                  },
                  "providerType":"LOCAL",
                  "supplierHotelId":6457589,
                  "isAlternative":false,
                  "optimizedThumbUrls":{
                     "srpDesktop":"https://thumbnails.trvl-media.com/uQm5oEf1djKsihiqX24-TfWhriQ=/250x140/smart/filters:quality(60)/images.trvl-media.com/hotels/7000000/6460000/6457600/6457589/b189dd23_z.jpg"
                  }
               },
               {
                  "id":756613600,
                  "name":"Astrotel Anonas",
                  "starRating":3,
                  "urls":[
                     
                  ],
                  "address":{
                     "streetAddress":"960 Aurora Boulevard",
                     "extendedAddress":"near LRT Anonas Station",
                     "locality":"Quezon City",
                     "postalCode":"",
                     "region":"",
                     "countryName":"Philippines",
                     "countryCode":"ph",
                     "obfuscate":false
                  },
                  "welcomeRewards":{
                     "collect":true
                  },
                  "guestReviews":{
                     "unformattedRating":4.8,
                     "rating":"4.8",
                     "total":40,
                     "scale":10
                  },
                  "landmarks":[
                     {
                        "label":"City centre",
                        "distance":"10 km"
                     },
                     {
                        "label":"Manila Bay",
                        "distance":"17 km"
                     }
                  ],
                  "geoBullets":[
                     
                  ],
                  "ratePlan":{
                     "price":{
                        "current":"P1,295",
                        "exactCurrent":1294.64,
                        "summary":"excluding taxes & fees"
                     },
                     "features":{
                        "freeCancellation":false,
                        "paymentPreference":false,
                        "noCCRequired":false
                     },
                     "type":"EC"
                  },
                  "neighbourhood":"Quezon City",
                  "deals":[
                     
                  ],
                  "messaging":[
                     
                  ],
                  "badging":[
                     
                  ],
                  "pimmsAttributes":"DoubleStamps|D13|TESCO",
                  "coordinate":{
                     "lat":14.627763,
                     "lon":121.06376
                  },
                  "providerType":"LOCAL",
                  "supplierHotelId":23612925,
                  "isAlternative":false,
                  "optimizedThumbUrls":{
                     "srpDesktop":"https://thumbnails.trvl-media.com/0GhZSM5K0XRDs9aaIYn2FuUBUqk=/250x140/smart/filters:quality(60)/images.trvl-media.com/hotels/24000000/23620000/23613000/23612925/c83bf0b0_z.jpg"
                  }
               }
            ]
         },
         "common":{
            "pointOfSale":{
               "numberSeparators":",.",
               "brandName":"Hotels.com"
            },
            "tracking":{
               "omniture":{
                  "s.prop33":"D=v33",
                  "s.prop32":"D=v43",
                  "s.prop74":"search result with dates | ms shown | N",
                  "s.products":"MULTISOURCE;6457589,LOCAL;23612925,LOCAL;23612485",
                  "s.eVar16":"Mob :: aApp",
                  "s.eVar40":"D=v0",
                  "s.eVar41":"PHP",
                  "s.eVar63":"60acf7e5-d622-4a1a-8110-a1fac036b405",
                  "s.eVar42":"30|1|20210404|20210405",
                  "s.eVar4":"|SI:anonymous|VS:returnVisitor|HCR:notApplicable|FC:notApplicable|NS:unknown|TI:notApplicable|SM:notApplicable|IR:anonymous|",
                  "s.eVar43":"en_PH|HCOM_PH|ph.hotels.com",
                  "s.eVar3":"2|0",
                  "s.eVar22":"5|PHP|24:438619:2382.7900:1286.7100:1|24:756613600:1294.6400:1294.6400:0|24:756599520:1294.6400:1294.6400:0|24:2052351936:1442.6200:1298.3600:0|24:454367:2407.3200:1299.9500:0",
                  "s.eVar2":"Manila, National Capital Region, Philippines",
                  "s.eVar23":"5|PHP|24:1279267456:1446.4300:1301.7900:0|24:632077728:2176.8300:1306.1000:1|24:494528:1455.3600:1309.8200:0|24:633744:1311.4800:1311.4800:0|24:1841636192:1311.4800:1311.4800:0",
                  "s.eVar24":"MCTC=20;TU=NA;PDID=NULL;MVT=",
                  "s.eVar7":"30",
                  "s.server":"ph.hotels.com",
                  "s.eVar6":"1004070",
                  "s.prop29":"D=v42",
                  "s.eVar5":"1",
                  "s.prop27":"1f6f9be9-a413-4f39-874c-86385cfe9627",
                  "s.eVar9":"Price - low to high",
                  "s.prop21":"0",
                  "s.prop20":"2",
                  "s.eVar69":"Mob :: aApp",
                  "s.currencyCode":"PHP",
                  "s.eVar26":"US:NY:NEWYORK:501",
                  "s.eVar29":"Mob :: aApp",
                  "s.prop9":"1286",
                  "s.eVar95":"Unknown",
                  "s.prop7":"1252",
                  "s.eVar31":"D=v0",
                  "s.eVar32":"D=v0",
                  "s.eVar33":"search result with dates",
                  "s.eVar34":"H1871:007.002,M376:152.000,M904:000.000,M3736:000.000,M4200:000.000,M4440.2,M4869:001.000,M4952:036.001,M4961:001.000,M5184:001.000,M5342:000.000,M5663:000.000,M6388:000.000,M6775:000.000,M6779:000.000,M7015.1,M7192:000.000,M7215:000.000,M7296:000.000,M7305:000.000,M7353:000.000,M7362.0,M7384:000.000,M7552:023.001,M7561:000.000,M7763:000.000,M7765.2,M7870:000.000,M7895:000.000,M8336:000.000,M8347:019.002,M8483:000.000,M8485:007.001,M8692:000.000,M8693:000.000,M8698:000.000,M8708:000.000,M8714:000.000,M8718:000.000,M8758:000.000,M8915:000.000,M8928:000.000,M8952:000.000,M8954:000.000,M8976:000.000,M8992:000.000,M9004:000.000,M9035:000.000,M9065:000.000,M9220:000.000,M9281:000.000,M9282:000.000,M9297:000.000,M9301:000.000,M9351:000.000,M9420:000.000,M9431:000.000,M9469:000.000,M9549:000.000,M9567.1,M9691:000.000,M9693:000.000,M9699:000.000,M9716:000.000,M9729:000.000,M9752:000.000,M9771:000.000,M9853:000.000,M9857:000.000,M9864:000.000,M9924:000.000,M9927:000.000,M9945:000.000,M9961:000.000,M9971:000.000,M10001:000.000,M10007:000.000,M10010:000.000,M10013:002.001,M10068:000.000,M10121:000.000,M10125:000.000,M10137:001.001,M10148:000.000,M10155:000.000,M10248:000.000,M9215:001.000",
                  "s.eVar13":"389242",
                  "s.prop19":"1",
                  "s.events":"event324,event319,event320",
                  "s.prop18":"D=v6",
                  "s.prop5":"389242",
                  "s.prop15":"1",
                  "s.prop3":"Manila, National Capital Region, Philippines",
                  "s.prop14":"D=v7",
                  "s.prop36":"|SI:anonymous|VS:returnVisitor|HCR:notApplicable|FC:notApplicable|NS:unknown|TI:notApplicable|SM:notApplicable|IR:anonymous|",
                  "s.eVar93":"aws.us-west-2.unknown",
                  "s.prop2":"Price - low to high"
               }
            }
         }
      }
   }
}

And here’s my php code.

                <?php  

                $filepath = '05-03-21.json'; 
                $json_string = file_get_contents($filepath); 
                $json_data = json_decode($json_string,true); 

                $json_data = json_decode($json_string,true); 
                foreach ($json_data as $var_1 ) {
                    foreach ($var_1['searchResults'] as $var_2 ) {
                        echo "Hotel Name: ".$var_2['name']."<br>";
                        echo "Ratings: ".$var_2['starRating']."<br>";
                        echo "Location: ".$var_2['address']['locality']."<br>";
                    }

                }

            ?>

All i wanna do is to display the Data with loop something like this. Image here

Advertisement

Answer

just parse the correct array with :

foreach ($json_data['data']['body']['searchResults']['results'] as $var_1 ) {
  echo "Hotel Name: ".$var_1['name']."<br>";
  echo "Ratings: ".$var_1['starRating']."<br>";
  echo "Location: ".$var_1['address']['locality']."<br />========<br />";
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement