Jump to content

Recommended Posts

I have a script that retrieves some info in the following format:

 

{"totalResultsCount":1,"geonames":[{"countryName":"United States","adminCode1":"CA","fclName":"spot, building, farm","countryCode":"US","lng":-117.1883677,"fcodeName":"airport","fcl":"S","name":"San Diego International Airport","fcode":"AIRP","geonameId":5391847,"lat":32.733384,"population":0,"adminName1":"California"}]}

 

i need to pick out lng and lat and store them as PHP variables...

 

not sure how..

 

EDIT: if it helps that is in JSON language...(not sure what that means)

Link to comment
https://forums.phpfreaks.com/topic/108081-parse-challenge/
Share on other sites

Not tested. Could probably be much faster/easier with regex, but I'm not very good with it (yet).

 

<?php
$string = <<<END
{"totalResultsCount":1, "geonames":[{"countryName":"United States",
"adminCode1":"CA", "fclName":"spot, building, farm", "countryCode":"US",
"lng":-117.1883677, "fcodeName":"airport", "fcl":"S",
"name":"San Diego International Airport", "fcode":"AIRP",
"geonameId":5391847, "lat":32.733384, "population":0,
"adminName1":"California"}]}
END;

$one = '"lat":'; $two = '"lng":';

$front1 = strpos($string, $one) + strlen($one);
$back1 = strpos($string, ",", $front1);
$front2 = strpos($string, $two) + strlen($two);
$back2 = strpos($string, ",", $front2);

$lat = substr($string, $front1, $back1 - $front1);
$long = substr($string, $front2, $back2 - $front2);
?>

 

JSON is JavaScript Object Notation. http://www.json.org/ I was messing around with ExtJS and it's used in with that a little bit. I think it's basically a way to store data. Don't know exactly.

Link to comment
https://forums.phpfreaks.com/topic/108081-parse-challenge/#findComment-554006
Share on other sites

try

<?php
$test = '{"totalResultsCount":1,"geonames":[{"countryName":"United States","adminCode1":"CA","fclName":"spot, building, farm","countryCode":"US","lng":-117.1883677,"fcodeName":"airport","fcl":"S","name":"San Diego International Airport","fcode":"AIRP","geonameId":5391847,"lat":32.733384,"population":0,"adminName1":"California"}]} ';
preg_match_all('/"(lat|lng)"[0-9.-]+)/', $test, $b);
$b = array_combine($b[1], $b[2]);
print_r($b);
?>

Link to comment
https://forums.phpfreaks.com/topic/108081-parse-challenge/#findComment-554043
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.