monkeypaw201 Posted May 31, 2008 Share Posted May 31, 2008 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) Quote Link to comment Share on other sites More sharing options...
monkeypaw201 Posted May 31, 2008 Author Share Posted May 31, 2008 I found a JSON to PHP parser.. http://mike.teczno.com/JSON/JSON.phps not sure if it helps.. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 31, 2008 Share Posted May 31, 2008 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. Quote Link to comment Share on other sites More sharing options...
sasa Posted May 31, 2008 Share Posted May 31, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.