Parsing 3rd level Nested JSON with PHP

I am showing to you how to JSON encode and get value of third nested level value using `foreach()`



<?php
        //Enter your code here, enjoy!

$content ='{"loan":{"loan_type":"1","type_accout":"saving","customer":[{"cus_id":"1","name":"FIRST MEMBER","bank":
[{"id":"1","loan_id":"1","loan_cust_id":"1","bank_name":"LOCAL"},
{"id":"2","loan_id":"1","loan_cust_id":"1","bank_name":"CENTER"}]},
{"cust_id":"2","name":"SECOND MEMBER","bank":[{"id":"3","loan_id":"1","loan_cust_id":"2","bank_name":"STANDARD"}]}]}}';


$json = json_decode($content, true);

    foreach($json['loan']['customer'] as $item => $value) {

       $name = $value['name']; // This works !

       foreach($json['loan']['customer'][$item]['bank'] as $bank_item) { 

            print_r($bank_item);
       }

}

About Example OUTPUT :

Array
(
    [id] => 1
    [loan_id] => 1
    [loan_cust_id] => 1
    [bank_name] => LOCAL
)
Array
(
    [id] => 2
    [loan_id] => 1
    [loan_cust_id] => 1
    [bank_name] => CENTER
)
Array
(
    [id] => 3
    [loan_id] => 1
    [loan_cust_id] => 2
    [bank_name] => STANDARD
)


DEMO

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...