1

I've look high and low for a solution for this, but haven't found anything that is exactly what I am running into. I am using php to parse a JSON response that is nested in a way that makes it difficult to extract specific fields.

Here is a bit of the JSON:

{
    "collection":{
    "items":[
        {
            "href":"https://api.teamsnap.com/v3/members/MEMBERID",
            "data":[
                "name":"id",
                "value": 0000000 // MEMBERID
            },
            {
                "name":"type",
                "value":"member"
            },
            {
               "name":"address_city",
               "value":""
            },
            {
               "name":"address_state",
               "value":""
            },

Here is my php:

$json = file_get_contents($url, false, $context);

$result = json_decode($json);
foreach ($result->collection->items as $items=>$val) {
    foreach ($val->data as $data=>$datasets) {
        foreach ($datasets as $dataset=>$val) {
            echo $dataset.': '.$val;
            echo "<br>";  
        }
    }
};

and this is a bit of the current output:

name: id
value: MEMBERID
name: first_name
value: MEMBERNAME
name: last_name
value: MEMBERNAME

what I want to do is be able to list all of the members in this response with their first and last name. I have tried using $val->first_name, $val['first_name']; and such but all result in a foreach error.

7
  • That JSON structure you posted is invalid. You cannot close an array opened with a square brace with a curly brace as done below the MEMBERID value. Commented Mar 13, 2017 at 6:44
  • That is just a sample and I changed the actual member id to protect security. I have updated the json in my code above Commented Mar 13, 2017 at 6:46
  • Certainly that is just an example. But still it is invalid. We cannot help to fix your code if the structure of the data is invalid, thus unclear. Commented Mar 13, 2017 at 6:48
  • You're probably missing an opening curly brace ({) before your first data member. Commented Mar 13, 2017 at 6:49
  • I am not writing the json, it is coming from another server. I am trying to parse the json with php. Commented Mar 13, 2017 at 6:50

1 Answer 1

3

If you can ensure every data member has a name and value field you can use something like this and skip a for-loop:

$json = file_get_contents($url, false, $context);

$result = json_decode($json);
foreach ($result->collection->items as $itemKey => $val) {
    $data = array();
    foreach ($val->data as $dataKey => $dataset) {
        $data[$dataset->name] = $dataset->value;
    }
    var_dump($data);
};

Although you should probably check for $result->collection and $result->collection->items first before looping over them to avoid errors.

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

5 Comments

That resulted in the following error: Cannot use a scalar value as an array
Be sure to check the variable names, I renamed the key in the foreach loop from $data to $dataKey - make sure you don't overwrite the array there.
Ok so that resulted in an array output of the entire string. How do I access a single variable like first_name?
It's just a normal array, just use $data["first_name"] after the loop, if it was part of the dataset it'll be in the $data array afterwards.
You sir, are the bomb!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.