[php] Parsing JSON array with PHP foreach

Wondering why my PHP code will not display all "Value" of "Values" in the JSON data:

$user = json_decode(file_get_contents($analytics));
foreach($user->data as $mydata)
{
     echo $mydata->name . "\n";

}        
foreach($user->data->values as $values)
{
     echo $values->value . "\n";
}

The first foreach works fine, but the second throws an error.

{
   "data": [
      {
         "id": "MY_ID/insights/page_views_login_unique/day",
         "name": "page_views_login_unique",
         "period": "day",
         "values": [
            {
               "value": 1,
               "end_time": "2012-05-01T07:00:00+0000"
            },
            {
               "value": 6,
               "end_time": "2012-05-02T07:00:00+0000"
            },
            {
               "value": 5,
               "end_time": "2012-05-03T07:00:00+0000"
            }, ...

This question is related to php foreach json

The answer is


You need to tell it which index in data to use, or double loop through all.

E.g., to get the values in the 4th index in the outside array.:

foreach($user->data[3]->values as $values)
{
     echo $values->value . "\n";
}

To go through all:

foreach($user->data as $mydata)
{
    foreach($mydata->values as $values) {
        echo $values->value . "\n";
    }

}   

$user->data is an array of objects. Each element in the array has a name and value property (as well as others).

Try putting the 2nd foreach inside the 1st.

foreach($user->data as $mydata)
{
    echo $mydata->name . "\n";
    foreach($mydata->values as $values)
    {
        echo $values->value . "\n";
    }
}

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to foreach

Angular ForEach in Angular4/Typescript? How to use forEach in vueJs? Python foreach equivalent Get current index from foreach loop TypeScript for ... of with index / key? JavaScript: Difference between .forEach() and .map() JSON forEach get Key and Value Laravel blade check empty foreach Go to "next" iteration in JavaScript forEach loop Why is "forEach not a function" for this object?

Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?