Upendra Sharma Upendra Sharma - 2 months ago 10
JSON Question

How to print json results when it has different form of data using php?

I am using third party api to get city lists into my dropdown. I am getting response from third party api in this form


"TopDestination":"14621

</cityId>Amsterdam

</cityName>NL

</countryCode>Netherlands

</countryName>AMS

</cityCode>


My response is in a variable called $res and when i am trying to print like this

$res->TopDestination->cityId;


But it is not printing anything. If i am trying to print

$res->TopDestination;


It is printing results like this

14621AmsterdamNLNetherlandsAMS

What should i use to print city id ?

My response from api is like this

{"Error":{"ErrorCode":0,"ErrorMessage":""},"Status":1,"TokenId":"59831ca1-37e8-42fd-8350-0b3699b86505","TopDestination":"14621<\/cityId>Amsterdam<\/cityName>NL<\/countryCode>Netherlands<\/countryName>AMS<\/cityCode><\/City>17249<\/cityId>Antalya<\/cityName>TR<\/countryCode>Turkey<\/countryName>ANTA<\/cityCode><\/City>9434<\/cityId>Athens<\/cityName>GR<\/countryCode>Greece<\/countryName>ATH<\/cityCode><\/City>10142<\/cityId>Bali<\/cityName>ID<\/countryCode>Indonesia<\/countryName>ABAL<\/cityCode><\/City>16974<\/cityId>Bangkok<\/cityName>TH<\/countryCode>Thailand<\/countryName>BKK<\/cityCode><\/City>3518<\/cityId>Barcelona<\/cityName>ES<\/countryCode>Spain<\/countryName>BCN<\/cityCode><\/City>23131<\/cityId>Basel<\/cityName>CH<\/countryCode>Switzerland<\/countryName>BSL<\/cityCode><\/City>23884<\/cityId>Beijing<\/cityName>CN<\/countryCode>China<\/countryName>PEK<\/cityCode><\/City>13551<\/cityId>Beirut<\/cityName>LB<\/countryCode>Lebanon<\/countryName>BEY<\/cityCode><\/City>1996<\/cityId>Berlin<\/cityName>DE<\/countryCode>Germany<\/countryName>BER<\/cityCode><\/City>24602<\/cityId>Brussels<\/cityName>BE<\/countryCode>Belgium<\/countryName>BRU<\/cityCode><\/City>10085<\/cityId>Budapest<\/cityName>HU<\/countryCode>Hungary<\/countryName>BUD<\/cityCode><\/City>2418<\/cityId>Cairo<\/cityName>EG<\/countryCode>Egypt<\/countryName>CAI<\/cityCode><\/City>32443<\/cityId>Cape Town<\/cityName>ZA<\/countryCode>South Africa<\/countryName>CPT<\/cityCode><\/City>20491<\/cityId>Chicago<\/cityName>US<\/countryCode>United States<\/countryName>CHI<\/cityCode><\/City>125<\/cityId>Cologne<\/cityName>DE<\/countryCode>Germany<\/countryName>CGN<\/cityCode><\/City>13616<\/cityId>Colombo<\/cityName>LK<\/countryCode>Sri Lanka<\/countryName>CMB<\/cityCode><\/City>2142<\/cityId>Copenhagen<\/cityName>DK<\/countryCode>Denmark<\/countryName>CPH<\/cityCode><\/City>25921<\/cityId>Dubai<\/cityName>AE<\/countryCode>United Arab Emirates<\/countryName>DXB<\/cityCode><\/City>32515<\/cityId>Durban<\/cityName>ZA<\/countryCode>South Africa<\/countryName>DUR<\/cityCode><\/City>27352<\/cityId>Dusseldorf<\/cityName>DE<\/countryCode>Germany<\/countryName>DUS<\/cityCode><\/City>8030<\/cityId>Edinburgh<\/cityName>GB<\/countryCode>United Kingdom<\/countryName>EDI<\/cityCode><\/City>11213<\/cityId>Florence<\/cityName>IT<\/countryCode>Italy<\/countryName>FLR<\/cityCode><\/City>22753<\/cityId>Frankfurt<\/cityName>DE<\/countryCode>Germany<\/countryName>FRA<\/cityCode><\/City>23435<\/cityId>Geneva<\/cityName>CH<\/countryCode>Switzerland<\/countryName>GVA<\/cityCode><\/City>26698<\/cityId>Gold Coast<\/cityName>AU<\/countryCode>Australia<\/countryName>OOL<\/cityCode><\/City>23960<\/cityId>Guangzhou<\/cityName>CN<\/countryCode>China<\/countryName>CAN<\/cityCode><\/City>379<\/cityId>Hamburg<\/cityName>DE<\/countryCode>Germany<\/countryName>HAM<\/cityCode><\/City>9926<\/cityId>Hong Kong<\/cityName>HK<\/countryCode>Hong Kong<\/countryName>HKG<\/cityCode><\/City>26996<\/cityId>Innsbruck<\/cityName>AT<\/countryCode>Austria<\/countryName>INN<\/cityCode><\/City>23037<\/cityId>Interlaken<\/cityName>CH<\/countryCode>Switzerland<\/countryName>INTE<\/cityCode><\/City>17306<\/cityId>Istanbul<\/cityName>TR<\/countryCode>Turkey<\/countryName>IST<\/cityCode><\/City>32569<\/cityId>Johannesburg<\/cityName>ZA<\/countryCode>South Africa<\/countryName>JNB<\/cityCode><\/City>15054<\/cityId>Kathmandu<\/cityName>NP<\/countryCode>Nepal<\/countryName>KTM<\/cityCode><\/City>14386<\/cityId>Kuala Lumpur<\/cityName>MY<\/countryCode>Malaysia<\/countryName>KUL<\/cityCode><\/City>35827<\/cityId>Kuwait<\/cityName>KW<\/countryCode>Kuwait<\/countryName>KWI<\/cityCode><\/City>14285<\/cityId>Langkawi<\/cityName>MY<\/countryCode>Malaysia<\/countryName>LANK<\/cityCode><\/City>32827<\/cityId>Las Vegas<\/cityName>US<\/countryCode>United States<\/countryName>LAS<\/cityCode><\/City>9245<\/cityId>London<\/cityName>GB<\/countryCode>United Kingdom<\/countryName>LON<\/cityCode><\/City>34549<\/cityId>London<\/cityName>CA<\/countryCode>Canada<\/countryName>YXU<\/cityCode><\/City>18631<\/cityId>Los Angeles<\/cityName>US<\/countryCode>United States<\/countryName>LAX<\/cityCode><\/City>23273<\/cityId>Lucerne<\/cityName>CH<\/countryCode>Switzerland<\/countryName>LUCE<\/cityCode><\/City>13674<\/cityId>Macau<\/cityName>MO<\/countryCode>Macau<\/countryName>MFM<\/cityCode><\/City>3000<\/cityId>Madrid<\/cityName>ES<\/countryCode>Spain<\/countryName>MAD<\/cityCode><\/City>14254<\/cityId>Mauritius<\/cityName>MU<\/countryCode>Mauritius<\/countryName>MRUW<\/cityCode><\/City>26916<\/cityId>Melbourne<\/cityName>AU<\/countryCode>Australia<\/countryName>MEL<\/cityCode><\/City>11297<\/cityId>Milan<\/cityName>IT<\/countryCode>Italy<\/countryName>MIL<\/cityCode><\/City>266<\/cityId>Munich<\/cityName>DE<\/countryCode>Germany<\/countryName>MUC<\/cityCode><\/City>13443<\/cityId>Nairobi<\/cityName>KE<\/countryCode>Kenya<\/countryName>NBO<\/cityCode><\/City>28985<\/cityId>New York<\/cityName>US<\/countryCode>United States<\/countryName>NYC<\/cityCode><\/City>4419<\/cityId>Nice<\/cityName>FR<\/countryCode>France<\/countryName>NCE<\/cityCode><\/City>19533<\/cityId>Orlando<\/cityName>US<\/countryCode>United States<\/countryName>ORL<\/cityCode><\/City>4775<\/cityId>Paris<\/cityName>FR<\/countryCode>France<\/countryName>PAR<\/cityCode><\/City>36773<\/cityId>Pattaya Beach<\/cityName>TH<\/countryCode>Thailand<\/countryName>PATT<\/cityCode><\/City>14321<\/cityId>Penang<\/cityName>MY<\/countryCode>Malaysia<\/countryName>PEN<\/cityCode><\/City>17093<\/cityId>Phuket<\/cityName>TH<\/countryCode>Thailand<\/countryName>PHUW<\/cityCode><\/City>24332<\/cityId>Prague<\/cityName>CZ<\/countryCode>Czech Republic<\/countryName>PRG<\/cityCode><\/City>12771<\/cityId>Rome<\/cityName>IT<\/countryCode>Italy<\/countryName>ROM<\/cityCode><\/City>27022<\/cityId>Salzburg<\/cityName>AT<\/countryCode>Austria<\/countryName>SZG<\/cityCode><\/City>18857<\/cityId>San Diego<\/cityName>US<\/countryCode>United States<\/countryName>SAN<\/cityCode><\/City>18861<\/cityId>San Francisco<\/cityName>US<\/countryCode>United States<\/countryName>SFO<\/cityCode><\/City>23909<\/cityId>Shanghai<\/cityName>CN<\/countryCode>China<\/countryName>SHA<\/cityCode><\/City>23976<\/cityId>Shenzhen<\/cityName>CN<\/countryCode>China<\/countryName>SHEN<\/cityCode><\/City>16532<\/cityId>Singapore<\/cityName>SG<\/countryCode>Singapore<\/countryName>SIN<\/cityCode><\/City>34326<\/cityId>Sydney<\/cityName>AU<\/countryCode>Australia<\/countryName>SYD<\/cityCode><\/City>13420<\/cityId>Tokyo<\/cityName>JP<\/countryCode>Japan<\/countryName>TYO<\/cityCode><\/City>34661<\/cityId>Toronto<\/cityName>CA<\/countryCode>Canada<\/countryName>YTO<\/cityCode><\/City>25561<\/cityId>Vancouver<\/cityName>CA<\/countryCode>Canada<\/countryName>YVR<\/cityCode><\/City>11501<\/cityId>Venice<\/cityName>IT<\/countryCode>Italy<\/countryName>VCE<\/cityCode><\/City>27059<\/cityId>Vienna<\/cityName>AT<\/countryCode>Austria<\/countryName>VIE<\/cityCode><\/City>23418<\/cityId>Zermatt<\/cityName>CH<\/countryCode>Switzerland<\/countryName>ZERM<\/cityCode><\/City>23424<\/cityId>Zurich<\/cityName>CH<\/countryCode>Switzerland<\/countryName>ZRH<\/cityCode><\/City><\/Cities>"}

Answer

The problem is with the JSON response from the API. Your array $res['TopDestination'] contains just a string, thus you cannnot access "cityId". You have to PROCESS the $res['TopDestination'].

Based on your JSON this should work (it's tested!):

<?php
// $json = result from API (a valid JSON string!)
$res=json_decode($json, true);

// get TopDestination string into an array
$data=explode("</City>", $res["TopDestination"]);
array_pop($data);
// create a new Database of cities
$cities=array();

foreach ($data as $d) {
  $tmp=explode("</cityId>",$d);
  $city_id=$tmp[0];
  $tmp=explode("</cityName>", $tmp[1]);
  $city_name=$tmp[0];
  $tmp=explode("</countryCode>", $tmp[1]);
  $country_code=$tmp[0];
  $tmp=explode("</countryName>", $tmp[1]);
  $country_name=$tmp[0];
  $city_code=$tmp[1];

  // add the city info in the database
  $cities[]=array(
    "cityId" => $city_id,
    "cityName" => $city_name,
    "cityCode" => $city_code,
    "countryCode" => $country_code,
    "countryName" => $country_name,
  );
}

// print the database array to check it
echo "<pre>"; 
print_r($cities);
echo "</pre>";

?>

The result is... :)

Array
(
[0] => Array
    (
        [cityId] => 14621
        [cityName] => Amsterdam
        [cityCode] => AMS
        [countryCode] => NL
        [countryName] => Netherlands
    )

[1] => Array
    (
        [cityId] => 17249
        [cityName] => Antalya
        [cityCode] => ANTA
        [countryCode] => TR
        [countryName] => Turkey
    )

[2] => Array
    (
        [cityId] => 9434
        [cityName] => Athens
        [cityCode] => ATH
        [countryCode] => GR
        [countryName] => Greece
    )

[3] => Array
    (
...