0

Im not sure if this would be considered proper format of a json structure. Essentially what Im trying to have is an array of associative arrays. My json data:

{
"notifications": [{
    "notificationid": "7hstyans",
    "notificationtitle": "Some alert title",
    "notificationtype": "SPECIAL ALERT",
    "dateCreated": "1502203175"
}, {
    "notificationid": "9ksh7dh2",
    "notificationtitle": "This is a old notification",
    "notificationtype": "OLD ALERT",
    "dateCreated": "1502138431"
}, {
    "notificationid": "iksnudo3",
    "notificationtitle": "new notification",
    "notificationtype": "SOME ALERT",
    "dateCreated": "1501000523"
}]
}

I am looking for a way to access it such as data.notifications[0]["notificationid"]

Would this be considered the correct format for json and the correct way to access it or what would be the best approach to format this?

3
  • If you want to know if you have valid JSON, just check it in JSONLint. And if you want to know if a certain way of doing something works, try it yourself. When you run what you suggested, does it work? Yes? Well, there's your answer. Commented Oct 4, 2017 at 14:01
  • data.notifications[0].notificationid Commented Oct 4, 2017 at 14:01
  • read about json_decode , either to associative array, or to stdClass. But : not both as you have it here. Pick one, decode your json, then use the appropriate syntax for the decode-style you picked. Commented Oct 4, 2017 at 14:03

1 Answer 1

2

Just use json_decode. Example:

$data = json_decode($jsonString, true);
echo $data['notifications'][0]['notificationid'];
Sign up to request clarification or add additional context in comments.

1 Comment

Usually "json_decode" decodes the json_string into an object. What "true" param does here is that it outputs the json_string into an associative array.

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.