Sahyog Vishwakarma
Sahyog Vishwakarma

Reputation: 410

How to fetch Array from .json in PHP

I have json document with Indian States[Cities[Area]], similar to the following:

{
    "Andaman and Nicobar Islands": [{
            "Port Blair": [
                "Aberdeen Bazaar",
                "Bidhabad Village",
                "Chidiyatapu",
                "Corbyns Cove",
                "Dollygunj",
                "Ferrurgunj",
                "Goalghar",
                "Goodwill Estate",
                "Junglighat",
                "Marine Hill",
                "Phoenix Bay",
                "South Point"
            ]
        },
        {
            "Havelock Island": [
                "Govindnagar Beach",
                "Radhanagar Beach",
                "Vijaynagar Beach"
            ]
        },
        {
            "Neil Island": [
                "Ramnagar",
                "Bharat Pur",
                "Sitapur Beach",
                "Laxmanpur",
                "Neil Kendra"
            ]
        }....
}

I want to fetch this array in this format:

$stateData['Andaman and Nicobar Islands'] => [
    "Port Blair" => [
        "Aberdeen Bazaar" => "Aberdeen Bazaar",
        "Bidhabad Village" => "Bidhabad Village"
         .
         .
         .
    ]
]

and so on... I have this Json data in a json file and assign the value to a variable $stateData using $stateData = (array)json_decode(fread($file, filesize(public_path('india_state_city1.json'))));

Upvotes: 0

Views: 70

Answers (3)

Er. Amit Joshi
Er. Amit Joshi

Reputation: 645

long but simple solution i think

  1. in my opinion json_decode function will help with second parameter passed as true because it will convert every inner object to array
  2. by converting to array we can easily use the state,city and area name and if you want ordering even you can do that by key no of array

    <?php
            $data = '{
                  "Andaman and Nicobar Islands": [
                          {
        "Port Blair": [
            "Aberdeen Bazaar",
            "Bidhabad Village",
            "Chidiyatapu",
            "Corbyns Cove",
            "Dollygunj",
            "Ferrurgunj",
            "Goalghar",
            "Goodwill Estate",
            "Junglighat",
            "Marine Hill",
            "Phoenix Bay",
            "South Point"
        ]
    },
    {
        "Havelock Island": [
            "Govindnagar Beach",
            "Radhanagar Beach",
            "Vijaynagar Beach"
        ]
    },
    {
        "Neil Island": [
            "Ramnagar",
            "Bharat Pur",
            "Sitapur Beach",
            "Laxmanpur",
            "Neil Kendra"
              ]
          }
                ]
      }';
          $indian_states = json_decode($data,TRUE);
    
           foreach ($indian_states as $stateName=>$statesCities)
                    {
                    echo $stateName;
    
                    foreach ($statesCities as $citiNo=>$cityAreas)
                           {
                                echo '<br />&nbsp;&nbsp;&nbsp;&nbsp;|---'; //comment this line
                                foreach ($cityAreas  as $citiName=>$areas)
                                       {
                                           echo $citiName;
    
                                           foreach ($areas as $areaName){
                                                      echo "<br     />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|---"; //comment this line                                                 echo $areaName;echo '<br />';
                             }
                         }
                  }
             }
    
          ?>
    

output

enter image description here

Upvotes: 0

Sajad Haibat
Sajad Haibat

Reputation: 717

I assume that your json is saved in a variable called $states.

  foreach (json_decode($states) as $key => $state) {
            foreach ($state as $stateData) {
                foreach ($stateData as $key2 => $city) {
                    foreach ($city as $key3 => $cit) {

                        $res[$key][$key2][$cit] = $cit;
                    }
                }

            }
        }

The output:

enter image description here

Check the Demo: https://paiza.io/projects/NlK9g5jluy8Lz9kmhMW69Q

Upvotes: 1

Mohamed-Foly
Mohamed-Foly

Reputation: 1

i got that you want to make the area array to be associative and the id,value is same. i f i'm right you can do this :

foreach ($stateData['Andaman and Nicobar Islands'] as &$array_1) {
    foreach ($array_1 as &$area_array){
        $formatted_array = [];
        foreach ($area_array as $area){
            $formatted_array[$area] = $area;
        }
        $area_array = $formatted_array;
    }
}

Upvotes: 0

Related Questions