5

I'm trying to get the value from this following JSON array in a PHP variable.

This is a var_dump of the array:

array(3) {
  ["id"]=>
  string(2) "14"
  ["css"]=>
  string(400) ""
  ["json"]=>
  string(4086) "
            {
                "Canvas": [
                    {
                        "MainObjects": {
                            "After Participation": {
                                "afterParticipationHeader": "Thank you!"
                            },
                            "Invite Friends": {
                                "InviteHeadline": "",
                                "InviteMsg": "",
                                "InviteImg": ""
                            }
                        },
                        "QuizModule": {
                            "Questions": [],
                            "Submit_Fields": [
                                {
                                    "label": "Name",
                                    "name": "txtName",
                                    "value": true
                                }
                            ]
                        }
                    }
                ]
            }"
        }

I am able to get the values for ["json"] in PHP like:

$json = $data[0]['json'];

But how do I get the value from from the array inside "json", like "AfterParticipationHeader". And "Submit_Fields" ?

Kim
  • 1,128
  • 6
  • 21
  • 41

4 Answers4

11

First you have to decode your json data

$json = json_decode($data[0]['json']);

Then you can access your AfterParticipationHeader

$json->Canvas[0]->MainObjects->{"After Participation"}->afterParticipationHeader
som
  • 4,650
  • 2
  • 21
  • 36
4

you can convert a valid JSON string to a PHP variable with json_decode(). Note the second parameter to get an assoc array instead of the less usefull stdClass.

$jsonData = json_decode($data[0]['json'], true);
$header = $jsonData['Canvas']['MainObjects']['After Participation']['afterParticipationHeader'];
NDM
  • 6,731
  • 3
  • 39
  • 52
2

You can decode the JSON via the json_decode function:

$json = json_decode($data[0]['json']);

Then, you'll have arrays (in the same structure) with your data.

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
1

It looks like you need to decode it. Try using: $json = json_decode($data[0]['json']);

Let me know if this helps.

dan
  • 136
  • 8