Search code examples
phpjsonjsondecoder

Parse Json and get result in php


I have a json data i am trying to get longitude and latitude from that json.

here is the JSON code

{
"0": {
    "provider": "gps",
    "time": 1517235370666,
    "latitude": 31.501278877258,
    "longitude": 74.366261959076,
    "accuracy": 63,
    "speed": 2.1400001049042,
    "altitude": 181,
    "bearing": 28.125,
    "locationProvider": 0
    }
}

I want Latitude and Longitude. I parse it with json_decode but still i am unable to get.
After Parse

 stdClass Object
(
[0] => stdClass Object
    (
        [provider] => gps
        [time] => 1517235370666
        [latitude] => 31.501278877258
        [longitude] => 74.366261959076
        [accuracy] => 63
        [speed] => 2.1400001049042
        [altitude] => 181
        [bearing] => 28.125
        [locationProvider] => 0
    )
)

Solution

  • try this

    $a = '{
    "0": {
        "provider": "gps",
        "time": 1517235370666,
        "latitude": 31.501278877258,
        "longitude": 74.366261959076,
        "accuracy": 63,
        "speed": 2.1400001049042,
        "altitude": 181,
        "bearing": 28.125,
        "locationProvider": 0
        }
    }';
    
    $b = json_decode($a); // returns object
    print_r($b->{'0'}->{'latitude'}); // to get latitude value
    
     $c = json_decode($a,true); // returns array
     print_r($c[0]['latitude']); // to get latitude value