简体   繁体   中英

how to fetch nested array data from json decode?

I have a JSON response that I need to parse in PHP. Here is the raw JSON:

{
    "status": 200,
    "status_message": "Route Successfull",
    "data": [
        [
            ["Karachi Company", "33.690166", "73.030357", "1-C"],
            ["Peshawar Mor", "33.684509", "73.047585", "1-C"],
            ["Federal Government College", "33.671909", "73.056038", "1-C"],
            ["I\/9 Police Station", "33.660728", "73.064430", "1-C"],
            ["Pindora Chungi", "33.652046", "73.064301", "1-C"],
            ["Double Road", "33.655663", "73.070930", "110"], 14
        ],
        [
            ["Karachi Company", "33.690166", "73.030357", "115"],
            ["Bohar Masjid", "33.691372", "73.040985", "115"],
            ["Peshawar Mor", "33.684509", "73.047585", "115"],
            ["H-9 Education", "33.674938", "73.053757", "115"],
            ["H-9 College", "33.671967", "73.056007", "115"],
            ["Dalda Mills", "33.663872", "73.049942", "115"],
            ["I-9 Chowk", "33.656727", "73.055077", "115"],
            ["I-9 Market", "33.652618", "73.053719", "115"],
            ["CDA Colony", "33.641186", "73.043297", "115"],
            ["Katarian Pull", "33.646591", "73.053673", "110"],
            ["Pindora Chungi", "33.652046", "73.064301", "110"],
            ["Double Road", "33.655663", "73.070930", "110"], 32
        ],
        [
            ["Karachi Company", "33.690166", "73.030357", "120"],
            ["FSD", "33.697506", "73.035721", "120"],
            ["Tipu Market", "33.700459", "73.041435", "120"],
            ["PIMS", "33.706490", "73.055450", "120"],
            ["Khyber Plaza", "33.708477", "73.056900", "120"],
            ["G-7\/2 Service Road", "33.703407", "73.058914", "120"],
            ["Sitara Market", "33.706654", "73.067314", "120"],
            ["Iqbal Hall", "33.709240", "73.074821", "120"],
            ["G-6\/2", "33.717533", "73.082520", "120"],
            ["Melody", "33.714916", "73.084656", "120"],
            ["Lal Masjid", "33.712467", "73.086685", "120"],
            ["Abpara", "33.708942", "73.089142", "120"],
            ["MNA Hostel", "33.726662", "73.090408", "120"],
            ["National Convention Center", "33.712711", "73.103676", "120"],
            ["Rawal Lake", "33.721302", "73.134445", "120"],
            ["Kashmir Chowk", "33.708317", "73.105339", "110"],
            ["Masjid Stop", "33.705292", "73.106873", "110"],
            ["Golf Club", "33.702656", "73.108376", "110"],
            ["Rawal Dam", "33.689449", "73.110977", "110"],
            ["Margalla Town", "33.678829", "73.104057", "110"],
            ["Faizabad", "33.663212", "73.084801", "110"],
            ["Double Road", "33.655663", "73.070930", "110"], 39
        ]
    ]
}

I tried doing

$result = json_decode($response);
$res = (array)$result[0]->data;
print_r($res);

but that seems to only display an empty array. I was expecting it to be the arrays mapped to the data key. What should I be doing to get those arrays?

With JavaScript

your JSON is a javascript object and you get data key's value with . (DOT) . (DOT) .

Answer: 1

var json = {}; // json data
var data=json.data; // now you can get any inner array with index.
var innerData=data[0];
var array=innerData[0]; //["Karachi Company", "33.690166", "73.030357", "1-C"]
console.log(array[0]); //Karachi Company

Answer: 2

function printJSON(json){
    var data=json.data;
    for(var i=0;i<data.length;i++){
        var innerData=data[i];
        for(var j=0;j<innerData.length;j++){
            console.log(innerData[j]);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM