用 PHP 解析(第三个)嵌套的 JSON

我正在尝试使用 PHP 解析以下 JSON,但在最后一级 ( "bank") 有一些问题,以下是信息:


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",

            }]

        }]

    }

}


最后一个foreach循环给出了错误说:


注意:未定义索引:银行


是否有关于可能导致问题的任何线索,或者是否有一些改进的解析相同 JSON 的方法,这将非常有帮助。


慕莱坞森
浏览 172回答 3
3回答

守候你守候我

您的 json 解析很好。您的访问缺少索引。由于“银行”位于“成员”数组中,因此您应该访问为$json['loan']['member'][0]['bank'](0 是硬编码的 - 您也可以切换为 1)。如果你使用 for 那么你应该这样做:foreach($json['loan']['member'] as $item) {     $name = $item['name']; // This works !   foreach($item['bank'] as $bank_item) { // use $item   }}

九州编程

你错过了那个成员也是多维数组$json = json_decode($content, true);&nbsp; &nbsp; /*&nbsp; &nbsp; echo "<pre>";&nbsp; &nbsp; print_r($json);&nbsp; &nbsp; echo "</pre>";*/&nbsp; &nbsp; foreach($json['loan']['member'] as $item => $value) {&nbsp; &nbsp; &nbsp; &nbsp;$name = $value['name']; // This works !&nbsp; &nbsp; &nbsp; &nbsp;foreach($json['loan']['member'][$item]['bank'] as $bank_item) { // THIS DOES NOT WORKS!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_r($bank_item);&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP