在火灾错误中,我查看我的 jquery 发布请求参数,就像这样
adults 1
applicants[] [object Object]
attendees 1
children 0
在这篇文章请求中,名为申请人的数组包含我想要迭代并在我的 codeigniter 控制器中提取值的 json 对象。json 字符串可能看起来像这样
({attendees:"2",
adults:"2",
children:"0",
grptype:"2",
'applicants[]':[{firstname:"John", lastname:"Doe", age:"33", allergies:"true", diabetic:"true", lactose:"false", note:"nuts"}, {firstname:"Jane", lastname:"Doe", age:"34", allergies:"true", diabetic:"false", lactose:"false", note:"pollen"}]
})
看看上面的申请者[],看到我有两个人的信息作为一个 json 对象。我不确定如何访问控制器中的数据。看到这个
$applicants = $this->input->post('applicants');
$this->output->append_output("<br/>Here: " . $applicants[0].firstname );
我在想 $applicants[0] 会引用 json 对象,我可以按需提取值。不确定我做错了什么。多谢你们。
编辑 所以我调整了我的json,它看起来像这样
adults 2
applicants[] {firstname:"John", lastname:"Doe", age:"23", allergies:"true", diabetic:"true", lactose:"false", note:"nuts"}
applicants[] {firstname:"Jane", lastname:"Doe", age:"23", allergies:"false", diabetic:"false", lactose:"false", note:""}
attendees 2
children 0
现在我仍然收到一个错误说
**Message: json_decode() expects parameter 1 to be string, array given**
有任何想法吗 ?
编辑 2
好的 mu 数据现在像这样
adults 1
applicants[] {"firstname": "John", "lastname": "Doe", "age": "34", "allergies": "true", "diabetic": "true", "lactose": "false", "note": "nuts"}
attendees 1
children 0
在控制器 ID 中这样做了
$applications = $this->input->post('applicants');
foreach ( $applications as $item)
{
$item = json_decode($item, true);
$this->output->append_output(print_r($item));
}
这是这个逻辑的结果
Array
(
[firstname] => John
[lastname] => Doe
[age] => 34
[allergies] => true
[diabetic] => true
[lactose] => false
[note] => nuts
)
不知道我做错了什么,无论我做什么来访问约会器,我都会收到一个错误,大意是我无法像那样访问它。如何提取值?