2

I have been trying to workout how to loop through and output the contents of a json file where field names start with "$" and keep getting an Undefined variable error message

Here is an example of the json file example (taken from https://mixpanel.com/help/reference/webhooks):

[
   {
      "$distinct_id":"13b20239a29335",
      "$properties":{
         "$region":"California",
         "$email":"[email protected]",
         "$last_name":"Bovik",
         "$created":"2012-11-20T15:26:16",
         "$country_code":"US",
         "$first_name":"Harry",
         "Referring Domain":"news.ycombinator.com",
         "$city":"Los Angeles",
         "Last Seen":"2012-11-20T15:26:17",
         "Referring URL":"http://news.ycombinator.com/",
         "$last_seen":"2012-11-20T15:26:19",
      }
   },
   {
      "$distinct_id":"13a00df8730412",
      "$properties":{
         "$region":"California",
         "$email":"[email protected]",
         "$last_name":"Lytics",
         "$created":"2012-11-20T15:25:38",
         "$country_code":"US",
         "$first_name":"Anna",
         "Referring Domain":"www.quora.com",
         "$city":"Mountain View",
         "Last Seen":"2012-11-20T15:25:39",
         "Referring URL":"http://www.quora.com/What-...",
         "$last_seen":"2012-11-20T15:25:42",
      }
   }
]

I am testing with a static string just to try and get things working. Here is my test code...

<?php

    $input = '[{"$distinct_id":"13b20239a29335","$properties":"dddd"}]';
    $jsonObj = json_decode($input, true);

    foreach ($jsonObj as $item) {
        foreach ($item as $rec) {
            echo '<br>';
            $my_id = $rec->$distinct_id;
            echo($my_id);
            $my_id = $rec->$properties;
            echo($my_id);
        }
        echo '<br>';
    }
?>

Any help would be appreciated.

Noob!

UPDATE: Musa gave this example which works for the single level json: foreach ($jsonObj as $item) {

    echo '<br>';
    $my_id = $item->{'$distinct_id'};
    echo($my_id);
    $my_id = $item->{'$properties'};
    echo($my_id);

echo '<br>';

}

How can this then be adapted to read and output all elements of the bigger multi-level json file?

1 Answer 1

5

Use Curly bracket notation

$object->{'$property'};

Edit

foreach ($jsonObj as $item) {

        echo '<br>';
        $my_id = $item->{'$distinct_id'};
        echo($my_id);
        foreach ($item->{'$properties'} as $my_prop => $value){
            echo("$my_prop => $value");
        }

    echo '<br>';
}

http://codepad.org/1cudZqlu

With the nested loop you're iterating the properties $distinct_id and $properties so $rec is actually a string and not an object.

Also your json is invalid as it has trailing , in the $properties field.

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

1 Comment

Thanks for that Musa. Much appreciated. How could I then loop through the deeper elements from the main json file?

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.