Get value from JSON array in PHP

I am trying to get the value from this next JSON array in a PHP variable.

This is the 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 can get the values ​​for ["json"] in PHP, for example:

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

But how do I get a value from an array inside "json", for example, "AfterParticipationHeader". And "Submit_Fields"?

+4
source share
4 answers

You must decode your json data first

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

Then you can access AfterParticipationHeader

 $json->Canvas[0]->MainObjects->{"After Participation"}->afterParticipationHeader 
+10
source

you can convert a valid JSON string to a PHP variable with json_decode () . Pay attention to the second parameter to get an associative array instead of the less useful stdClass .

 $jsonData = json_decode($data[0]['json'], true); $header = $jsonData['Canvas']['MainObjects']['After Participation']['afterParticipationHeader']; 
+3
source

You can decode JSON using json_decode function:

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

Then you will have arrays (in the same structure) with your data.

+2
source

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

Let me know if this helps.

+1
source

Source: https://habr.com/ru/post/1494747/


All Articles