zgrizzly_7
zgrizzly_7

Reputation: 793

how to Access to nested items in JSON with foreach PHP?

I have the following JSON structure

{"Id":"1","Persons":[{"Name":"Carl","Time":"00:00:03","info":"","Timeext":"","Timeout":"","Timein":""}, {"Name":"Carl","Time":"00:00:03","info":"","Timeext":"","Timeout":"","Timein":""}{"Name":"Luis","Time":"00:00:08","info":"","Timeext":"","Timeout":"","Timein":""}]}

How I can have acces or read the item inside the nest? For example if I just want the value of time for Carl or all information about Carl. Till now I just can get without a problem the single item in the collection 'Id'. The rst nested items not. I tryed with json_decode like this:

if( $_POST ) {

            $arr['Id'] = $_POST['Id'];
            $arr['NP'] = $_POST['NP'];

            $jsdecode = json_decode($arr);

            foreach ($jsdecode as $values){
                echo $values->Time;
            }

Can PLEASE somebody help me?

Upvotes: 1

Views: 65

Answers (2)

Tushar Gupta
Tushar Gupta

Reputation: 15913

You are processing it correct , Just add true as the second parameter to json_decode which will be converted to an Array like

$jsdecode = json_decode($arr,true);

Upvotes: 1

Lucky Chingi
Lucky Chingi

Reputation: 2258

if( $_POST ) {

        $arr['Id'] = $_POST['Id'];
        $arr['NP'] = $_POST['NP'];

        $jsdecode = json_decode($arr,true);

        foreach ($jsdecode as $values){
            echo $values->Time;
        }

Adding 'true' to json_decode converts it to array

Upvotes: 5

Related Questions