Skip to content
Advertisement

php foreach loop don’t work when I want to traverse json file

My foreach loop dont work. It brings me only 1st item info when i use $arr in my foreach loop but when i change that to $value it gives me nothing 🙁

<?php
      $url = file_get_contents("libs/json/countryBorders.geo.json");
      $arr = json_decode($url,true);
    
    
      foreach($arr as $value)  {
             echo $value['features'][0]['properties']['name'];
             echo $value['features'][0]['properties']['iso_a2'];
       };
        
?>

Here is one of my elements in json file. I want to print all names and iso_a2 codes

{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "properties":{
            "name":"Bahamas",
            "iso_a2":"BS",
            "iso_a3":"BHS",
            "iso_n3":"044"
         },
         "geometry":{
            "type":"MultiPolygon",
            "coordinates":[
               [
                  [
                     [
                        -77.53466,
                        23.75975
                     ],
                     [
                        -77.78,
                        23.71
                     ],
                     [
                        -78.03405,
                        24.28615
                     ],
                     [
                        -78.40848,
                        24.57564
                     ],
                     [
                        -78.19087,
                        25.2103
                     ],
                     [
                        -77.89,
                        25.17
                     ],
                     [
                        -77.54,
                        24.34
                     ],
                     [
                        -77.53466,
                        23.75975
                     ]
                  ]
               ],
               [
                  [
                     [
                        -77.82,
                        26.58
                     ],
                     [
                        -78.91,
                        26.42
                     ],
                     [
                        -78.98,
                        26.79
                     ],
                     [
                        -78.51,
                        26.87
                     ],
                     [
                        -77.85,
                        26.84
                     ],
                     [
                        -77.82,
                        26.58
                     ]
                  ]
               ],
               [
                  [
                     [
                        -77,
                        26.59
                     ],
                     [
                        -77.17255,
                        25.87918
                     ],
                     [
                        -77.35641,
                        26.00735
                     ],
                     [
                        -77.34,
                        26.53
                     ],
                     [
                        -77.78802,
                        26.92516
                     ],
                     [
                        -77.79,
                        27.04
                     ],
                     [
                        -77,
                        26.59
                     ]
                  ]
               ]
            ]
         }
      }

Advertisement

Answer

I didn’t read, that it is a list of your example object. So better follow the other answer :S

You have an JSON Object (it is not an array/list) and I wonder if that even works. You want to get the features of the path “/Features/” right? So you have to change it like the following:

<?php
      $url = file_get_contents("libs/json/countryBorders.geo.json");
      $arr = json_decode($url,true);
    
    
      foreach($arr['features'] as $feature)  {
             echo $feature['properties']['name'];
             echo $feature['properties']['iso_a2'];
      };
?>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement