1

I have JSON data as shown:

{
    "15": {
        "nid": "15",
        "status": "1",
        "created": "1352652530",
        "changed": "1353780901",
        "title": "some data",
        "type": "some data",
        "tracking_url": {
            "10": {
                "affiliate": "OMG",
                "url_part1": "some data"
            }
        },
        "primary_category": "Travel",
        "active_affiliate": "OMG"
    },
    "16": {
        "nid": "16",
        "status": "1",
        "created": "1352652530",
        "changed": "1353780901",
        "title": "some data",
        "type": "some data",
        "tracking_url": {
            "11": {
                "affiliate": "OMG",
                "url_part1": "some data"
            }
        },
        "primary_category": "Travel",
        "active_affiliate": "OMG"
    }
}

I can decode the JSON successfully using this:

$response = json_decode($p, true);

Now I have to pull the url_part1 and affiliate values from $response (extract elements inside tracking_url), which I am unable to do.

3
  • 1
    That's not a valid JSON text. Please show us a correct representative example. Commented Mar 16, 2013 at 0:00
  • 1
    Two curly braces in a row? That's not valid JSON... You should be getting false. Commented Mar 16, 2013 at 0:00
  • @MattBall : have Edited the question and included the link for original the JSON Commented Mar 16, 2013 at 0:10

1 Answer 1

1

Assuming the JSON structure in $p actually looks like this:

"16": {
    "nid":"16",

    "tracking_url": {
        "10": {
            "affiliate":"OMG",
            "url_part1":"some data"
        }
    }

},

You can access the url_part1 and affiliate with the following

$response = json_decode($p, true);

$urlPart = array();
$affiliate = array();

foreach ($response as $nid => $data) {
    $urlPart[$nid]   = $data['tracking_url']['10']['url_part1'];
    $affiliate[$nid] = $data['tracking_url']['10']['affiliate'];
}

This is just an example of how to access the data, you can write to any variable you need as applicable.

Sign up to request clarification or add additional context in comments.

2 Comments

what is the purpose of $nid => $data ??
$nid will get the name of the JSON object, ie "16": { ... }, then $data will get the data assigned to that object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.