0

I am trying to parse following JSON with PHP but at the very last level ("bank") having some issues, following is the information:

JSON:

{
    "loan": {
        "fu": "1046",
        "vb": "84",
        "loan_type": "1",
        "type_cocg": "14",
        "meeting_place": "PLACE",
        "meeting_date": "2019-05-29",
        "creation_date": "2019-05-29 12:49:53",
        "user_id": "1001-1556",
        "member": [{
            "mem_id": "1",
            "name": "FIRST MEMBER",
            "parentage": "PARENTAGE",
            "cnic": "3393399393393",
            "gender": "1",
            "dob": "1994-05-29",
            "marital_status": "1",
            "spouse_name": "SPOUSE",
            "spouse_cnic": "9939439939393",
            "pres_address": "PRES ADDRESS",
            "perma_address": "PERMA ADDRESS",
            "mother_name": "MOTHER NAME",
            "cell": "94494944949",
            "loan_amount": "30000",
            "network": "1",
            "sim_ownership": "2",
            "co_status": "3",
            "occupation_category": "2",
            "agri_occ": "null",
            "nonagri_occ": "3",
            "education": "1",
            "disability": "2",
            "religion": "6",
            "head": "2",
            "purpose": "2",
            "repayment_mode": "null",
            "duration": "4",
            "purpose_ent": "null",
            "purpose_agri": "null",
            "area_unit": "2",
            "agri_investment": "",
            "agri_expense": "",
            "purpose_livestock": "3",
            "loan_id_mem": "1",
            "monthly_income": "15000",
            "monthly_expense": "2000",
            "monthly_saving": "13000",
            "yearly_saving": "156000",
            "male": "2",
            "female": "2",
            "children": "2",
            "cow": "2",
            "buffalo": "2",
            "goat": "2",
            "sheep": "2",
            "agri_area_unit": "1",
            "land_own": "3",
            "land_lease": "3",
            "house_own": "3",
            "house_rent": "3",
            "caste": "CASTE",
            "active_loan": "1",
            "bank": [{
                "id": "1",
                "loan_id": "1",
                "loan_mem_id": "1",
                "bank_id": "1",
                "bank_loan": "",
                "bank_remaining": "2000",
                "purpose": "1",
                "purpose_agri": "16",
                "purpose_livestock": "null",
                "purpose_ent": "null"
            }, {
                "id": "2",
                "loan_id": "1",
                "loan_mem_id": "1",
                "bank_id": "6",
                "bank_loan": "",
                "bank_remaining": "500",
                "purpose": "3",
                "purpose_agri": "16",
                "purpose_livestock": "null",
                "purpose_ent": "14"
            }]
        }, {
            "mem_id": "2",
            "name": "SECOND MEMBER",
            "parentage": "PARENTAGE",
            "cnic": "3939939393399",
            "gender": "1",
            "dob": "1994-05-29",
            "marital_status": "1",
            "spouse_name": "SPOUSE",
            "spouse_cnic": "4949949494999",
            "pres_address": "ADDRESS",
            "perma_address": "ADDRESS",
            "mother_name": "MOTHER",
            "cell": "49494949494",
            "loan_amount": "20000",
            "network": "1",
            "sim_ownership": "2",
            "co_status": "2",
            "occupation_category": "2",
            "agri_occ": "null",
            "nonagri_occ": "2",
            "education": "1",
            "disability": "1",
            "religion": "1",
            "head": "1",
            "purpose": "1",
            "repayment_mode": "null",
            "duration": "3",
            "purpose_ent": "null",
            "purpose_agri": "16",
            "area_unit": "1",
            "agri_investment": "1500",
            "agri_expense": "2000",
            "purpose_livestock": "3",
            "loan_id_mem": "1",
            "monthly_income": "15000",
            "monthly_expense": "200",
            "monthly_saving": "14800",
            "yearly_saving": "177600",
            "male": "0",
            "female": "0",
            "children": "2",
            "cow": "2",
            "buffalo": "2",
            "goat": "2",
            "sheep": "2",
            "agri_area_unit": "1",
            "land_own": "3",
            "land_lease": "3",
            "house_own": "3",
            "house_rent": "2",
            "caste": "CASTE 2",
            "active_loan": "1",
            "bank": [{
                "id": "3",
                "loan_id": "1",
                "loan_mem_id": "2",
                "bank_id": "6",
                "bank_loan": "",
                "bank_remaining": "300",
                "purpose": "1",
                "purpose_agri": "43",
                "purpose_livestock": "null",
                "purpose_ent": "null"
            }]
        }]
    }
}

PHP code:

$json = json_decode($content, true);
$json['loan']['fu']; // This works !

foreach($json['loan']['member'] as $item) {
   $name = $item['name']; // This works !
   foreach($json['loan']['member']['bank'] as $bank_item) { // THIS DOES NOT WORKS!

   }
}

The last foreach loop gives out en error saying:

Notice: Undefined index: bank

Are there any clues as to what might be causing the issue, or is there some improved way of parsing the same JSON, that would be very helpful.

0

4 Answers 4

2

Your json parsing is fine. your accessing is missing index.

As the "bank" is inside an array of "member" you should access as $json['loan']['member'][0]['bank'] (the 0 is hard coded - you can switch for 1 also).

If you use for then you should do:

foreach($json['loan']['member'] as $item) {  
   $name = $item['name']; // This works !
   foreach($item['bank'] as $bank_item) { // use $item

   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use only a single foreach() and get the bank element value. If you further need to loop on bank element then you can use another foreach()

$json = json_decode($content, true);
foreach($json['loan']['member'] as $item) {
  print_r($item['bank']);
  foreach($item['bank'] as $bank_item) { 
      echo $bank_item;
   }
}

DEMO: https://3v4l.org/qB8mV

1 Comment

I tried this, it prints the array (banks) just fine with print_r, however to individually retrieve each variable inside the banks array, using $json['loan']['member'][0]['bank'] might be the better way (replacing 0 with the looping index value), Thanks nonetheless for your answer !
0

You have missed that member is also multidimensional array

$json = json_decode($content, true);
    /*
    echo "<pre>";
    print_r($json);
    echo "</pre>";*/
    foreach($json['loan']['member'] as $item => $value) {

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

       foreach($json['loan']['member'][$item]['bank'] as $bank_item) { // THIS DOES NOT WORKS!

            print_r($bank_item);
       }


    }

Comments

0

You can use array_walk_recursive() function also

$json = json_decode($content, true);
$i=0;
foreach($json['loan']['member'] as $item) { 
 array_walk_recursive($json['loan']['member'][$i]['bank'], function($value,$key) {
        echo  $key.' :'.$value ." \n";
    });
 $i++;    
}

DEMO : https://3v4l.org/KDR6V

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.