1

I'm trying to slice a multi-array in php. I'm using google's geo-location api and I've managed to get it to return lat,lng and accuracy. I've used

$message =  json_decode($result, true);

To convert the Json into an array. The problem I have is that I have the results in this form:

Array ( [location] => Array ( [lat] => 10.1453652 [lng] => 0.2338764 ) [accuracy] => 33 ) 

and I can't figure out how to extract the lat and lon. Can anyone help?

dWinder
  • 11,597
  • 3
  • 24
  • 39
matt3526
  • 41
  • 3
  • 2
    `echo $message["location"]["lat"]` for lat. I guess you can understand how to get the lng... – dWinder May 15 '19 at 13:19
  • You are a legend, thank you. It would be embarrassing for me to tell how how long I've been struggling with this, but let's just say it's been far too long! – matt3526 May 15 '19 at 13:23

2 Answers2

1

Try the following code:

echo $message['location']['lat'];

oreopot
  • 3,392
  • 2
  • 19
  • 28
  • Do or do not. There is no "try". A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard May 15 '19 at 13:50
0

It's simple, if not what you are expected, its straight forward

echo $message['location']['lat'].' '.$message['location']['lng'];

Working demo.

Rahul
  • 18,271
  • 7
  • 41
  • 60
  • The sandbox is so helpful, thanks. I'm learning a ton today – matt3526 May 15 '19 at 13:53
  • @matt3526 Happy to help. *To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in.* – Rahul May 15 '19 at 13:55