我正在尝试使用 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 的方法,这将非常有帮助。
3 回答
守候你守候我
TA贡献1802条经验 获得超10个赞
您的 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
}
}
九州编程
TA贡献1785条经验 获得超4个赞
你错过了那个成员也是多维数组
$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);
}
}
- 3 回答
- 0 关注
- 157 浏览
添加回答
举报
0/150
提交
取消