PHP   

How to parse multidimensional JSON arrays in PHP

This quick code snippet is about getting data from multidimensional JSON response using PHP arrays.  In this example we will be using sample API response from ipbase.com.

Example JSON stored in a PHP variable

$json = '{
   "data": {
      "ip": "103.201.151.82",
      "type": "v4",
      "range_type": {
         "type": "PUBLIC",
         "description": "Public address"
      },
      "connection": {
         "asn": 46562,
         "organization": "Example LLC",
         "isp": "Example server solutions"
      },
      "location": {
         "geonames_id": 6465532,
         "latitude": 49.28688049316406,
         "longitude": -123.11962127685547,
         "zip": "V6E 3S7",
         "continent": {
            "code": "NA",
            "name": "North America",
            "name_translated": "North America"
         },
         "country": {
            "alpha2": "CA",
            "alpha3": "CAN",
            "calling_codes": [
               "+1"
            ],
            "currencies": [
               {
                  "symbol": "CA$",
                  "name": "Canadian Dollar",
                  "symbol_native": "$",
                  "decimal_digits": 2,
                  "rounding": 0,
                  "code": "CAD",
                  "name_plural": "Canadian dollars"
               }
            ],
            "emoji": "🇨🇦",
            "ioc": "CAN",
            "languages": [
               {
                  "name": "English",
                  "name_native": "English"
               },
               {
                  "name": "French",
                  "name_native": "Français"
               }
            ],
            "name": "Canada",
            "name_translated": "Canada",
            "timezones": [
               "America/St_Johns",
               "America/Halifax",
               "America/Glace_Bay",
               "America/Moncton",
               "America/Goose_Bay",
               "America/Blanc-Sablon",
               "America/Toronto",
               "America/Nipigon",
               "America/Thunder_Bay",
               "America/Iqaluit",
               "America/Pangnirtung",
               "America/Atikokan",
               "America/Winnipeg",
               "America/Rainy_River",
               "America/Resolute",
               "America/Rankin_Inlet",
               "America/Regina",
               "America/Swift_Current",
               "America/Edmonton",
               "America/Cambridge_Bay",
               "America/Yellowknife",
               "America/Inuvik",
               "America/Creston",
               "America/Dawson_Creek",
               "America/Fort_Nelson",
               "America/Vancouver",
               "America/Whitehorse",
               "America/Dawson"
            ],
            "is_in_european_union": false,
            "fips": "CA",
            "geonames_id": "6251999",
            "hasc_id": "CA",
            "wikidata_id": "Q16"
         },
         "city": {
            "fips": null,
            "alpha2": null,
            "geonames_id": "6173331",
            "hasc_id": null,
            "wikidata_id": "Q24639",
            "name": "Vancouver",
            "name_translated": "Vancouver"
         },
         "region": {
            "fips": "CA02",
            "alpha2": "CA-BC",
            "geonames_id": "5909050",
            "hasc_id": "CA.BC",
            "wikidata_id": "Q1974",
            "name": "British Columbia",
            "name_translated": "British Columbia"
         }
      },
      "tlds": [
         ".ca"
      ],
      "timezone": {
         "id": "America/St_Johns",
         "current_time": "2022-12-04T07:17:02-03:30",
         "code": "NST",
         "is_daylight_saving": false,
         "gmt_offset": -12600
      },
      "security": {
         "is_anonymous": null,
         "is_bot": null,
         "is_known_attacker": null,
         "is_proxy": null,
         "is_spam": null,
         "is_tor": null,
         "proxy_type": null,
         "threat_score": null
      }
   }
}';

For sake of this example we are saving the static JSON string into the PHP variable but in the real world scenarios you will be getting this string from API responses. Now the main part of the code, how to read the nested JSON strings using multidimensional PHP arrays. The key points for this to work are json_decode and the foreach loop to iterate through array values. A complete PHP code snippet to read the data from the complex JSON strings like above example is given below.

$arr = json_decode($json, true);
foreach ($arr as $item){
    echo 'IP Address - '.$item['ip'].'<br>';
    echo 'IP Address Type - '.$item['type'].'<br>';
    echo 'Connection Organisation - '.$item['connection']['organization'].'<br>';
    echo 'Latitue - '.$item['location']['latitude'].'<br>';
    echo 'Longitude - '.$item['location']['longitude'].'<br>';
    echo 'Country Code - '.$item['location']['country']['alpha2'].'<br>';

    foreach($item['location']['country']['currencies'] as $currency){
        echo 'Currency Code - '.$currency['code'].'<br>';
        echo 'Currency - '.$currency['name'].'<br>';
    }

    foreach($item['location']['country']['timezones'] as $timezone){
        echo $timezone.'<br>';
    }

    foreach($item['location']['country']['languages'] as $language){
        echo 'Language Name - '.$language['name'].'<br>';
        echo 'Language Name Native - '.$language['name_native'].'<br>';
    }

    foreach($item['security'] as $key => $security){
        echo $key.' - '.$security.'<br>';
    }
}
Need a helping hand in fixing your website issues?

If you are facing any problems in implementing these code snippets and tutorials, you can hire us to fix your website issues.

Hire Us