Jump to content

having issues getting specific data in a json. im sure it's easy for those that use json more than i do


Go to solution Solved by gizmola,

Recommended Posts

[
    {
        "place_id": 329211526,
        "licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
        "osm_type": "way",
        "osm_id": 19239538,
        "boundingbox": [
            "34.6786854",
            "34.6787122",
            "-77.5843465",
            "-77.5835107"
        ],
        "lat": "34.6787013",
        "lon": "-77.5840059",
        "display_name": "West Main Street, lex, Troy Township, BIGG County, Ohio, 44333, United States",
        "class": "highway",
        "type": "primary",
        "importance": 0.6000099999999999
    },
    {
        "place_id": 329211743,
        "licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
        "osm_type": "way",
        "osm_id": 38610646,
        "boundingbox": [
            "34.6787122",
            "34.6871188",
            "-77.603118",
            "-77.5843465"
        ],
        "lat": "34.6827021",
        "lon": "-77.5937807",
        "display_name": "West Main Street, lex, Troy Township, BIGG County, Ohio, 44333, United States",
        "class": "highway",
        "type": "secondary",
        "importance": 0.6000099999999999
    },
    {
        "place_id": 329180475,
        "licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
        "osm_type": "way",
        "osm_id": 823017968,
        "boundingbox": [
            "34.9201281",
            "34.9214493",
            "-81.1346294",
            "-81.1251686"
        ],
        "lat": "34.9208004",
        "lon": "-81.1297602",
        "display_name": "West Main Street, Alliance, lex Township, Stark County, Ohio, 44601, United States",
        "class": "highway",
        "type": "tertiary",
        "importance": 0.6000099999999999
    },
    {
        "place_id": 329179580,
        "licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
        "osm_type": "way",
        "osm_id": 19258266,
        "boundingbox": [
            "34.9095347",
            "34.9189094",
            "-81.1539336",
            "-81.134635"
        ],
        "lat": "34.9146373",
        "lon": "-81.1445334",
        "display_name": "West Main Street, Alliance, lex Township, Stark County, Ohio, 44601, United States",
        "class": "highway",
        "type": "unclassified",
        "importance": 0.6000099999999999
    }
]

all i want to do is get the values of "lat" and "lon" from the first section (inside the first "{ }" ).  so i can do:

$lat=34.6787013;

$lon=-77.5840059;

 

thanks for your help in advanced.  ive tried looking up objects ect..  im not even sure what they are.  is lat and lon objects ??

  • Solution

Here you go:

$json = '[
    {
        "place_id": 329211526,
        "licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
        "osm_type": "way",
        "osm_id": 19239538,
        "boundingbox": [
            "34.6786854",
            "34.6787122",
            "-77.5843465",
            "-77.5835107"
        ],
        "lat": "34.6787013",
        "lon": "-77.5840059",
        "display_name": "West Main Street, lex, Troy Township, BIGG County, Ohio, 44333, United States",
        "class": "highway",
        "type": "primary",
        "importance": 0.6000099999999999
    },
    {
        "place_id": 329211743,
        "licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
        "osm_type": "way",
        "osm_id": 38610646,
        "boundingbox": [
            "34.6787122",
            "34.6871188",
            "-77.603118",
            "-77.5843465"
        ],
        "lat": "34.6827021",
        "lon": "-77.5937807",
        "display_name": "West Main Street, lex, Troy Township, BIGG County, Ohio, 44333, United States",
        "class": "highway",
        "type": "secondary",
        "importance": 0.6000099999999999
    },
    {
        "place_id": 329180475,
        "licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
        "osm_type": "way",
        "osm_id": 823017968,
        "boundingbox": [
            "34.9201281",
            "34.9214493",
            "-81.1346294",
            "-81.1251686"
        ],
        "lat": "34.9208004",
        "lon": "-81.1297602",
        "display_name": "West Main Street, Alliance, lex Township, Stark County, Ohio, 44601, United States",
        "class": "highway",
        "type": "tertiary",
        "importance": 0.6000099999999999
    },
    {
        "place_id": 329179580,
        "licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
        "osm_type": "way",
        "osm_id": 19258266,
        "boundingbox": [
            "34.9095347",
            "34.9189094",
            "-81.1539336",
            "-81.134635"
        ],
        "lat": "34.9146373",
        "lon": "-81.1445334",
        "display_name": "West Main Street, Alliance, lex Township, Stark County, Ohio, 44601, United States",
        "class": "highway",
        "type": "unclassified",
        "importance": 0.6000099999999999
    }
]';

$data = json_decode($json, true);

echo "latitude: {$data[0]['lat']} longitude: {$data[0]['lon']}";

 

Things to understand:

  • json_decode can either create an array of php objects or a php array.  Choosing array typically is simpler.  Thus I passed true as the 2nd parameter to json_decode().
  • Javascript arrays (as they can now be represented in php as well) are enclosed in square brackets.  [  ... this is an array of stuff ]
    • The first structure in the array will array key 0
    • In this case your data is an array of javascript objects (the "name": value pairs)
    • From there, all you need to do is reference the key value for the element you want.

Therefore you get $data[0]['lat'] for the first array element 'lat' key from the original json object.

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.