0

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
    )
)
M Arfan
  • 4,384
  • 4
  • 29
  • 46
  • 3
    What are you talking about?? Both `latitude` and `longitude` are **right** there. `echo json_decode( $json )->{0}->latitude;` – MonkeyZeus Jan 29 '18 at 14:32
  • Can you show the way you are trying to access the `latitude` and `longitude` ? if you simply `json_encode($array)`, everything is transformed in `stdClass`. You may want to the second parameter `json_encode($array, true)`which would transform this as array instead of stdClass. – Unex Jan 29 '18 at 14:34
  • i am doing json_decode and get the result as i above in question. – M Arfan Jan 29 '18 at 14:37
  • 1
    There is so many choices to pick from when doing a search of "get value from json in php"... – IncredibleHat Jan 29 '18 at 14:50

1 Answers1

3

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
Arun Kumaresh
  • 6,211
  • 6
  • 32
  • 50