phpdev phpdev - 1 month ago 5
JSON Question

how to retrieve data from json in json

I have following code in my contoller:

public function actionForm($id)
{
$jsonfile=ChForms::model()->findByPk($id);
$decodedJson=json_decode($jsonfile->json, true);
var_dump($decodedJson);
return $decodedJson;
}


Here json ( in $jsonfile->json) is name of the field in ChForms.
When I wrote `var_dump($decodedJson);, it showed the data below:

array(38) { ["name"]=> string(10) "membership" ["view"]=> string(14) "pdf/membership" ["phone"]=> string(5) "22222"


I am going to display it in the following way:

name: membership
view: pdf/membership
phone: 22222


How can I do it?

Answer

You can use foreach():

foreach ($decodedJsonas as $key => $value) {
        echo "$key: $value \n";
    }

You can see how it works below:

PHP Compiler

Comments