我在Codeigniter工作,并尝试使用jQuery.parseJSON
解析ajax成功方法数据,但无法正确解析数据。通过循环填充PHP数组,然后将该数组返回给ajax响应。这就是结果数据。
[
[
{
"PP_id":"1",
"PP_purchase":"1",
"PP_imei":"12112",
"PP_purchasePrice":"100",
"PP_salePrice":"200",
"PP_color":"red",
"PP_memory":"8",
"PP_memoryUnit":"MB",
"PP_sim":"Dual",
"PP_modifiedBy":null,
"PP_saleStatus":"1",
"PP_saleId":"1",
"User_id":"1",
"deleted":"0"
}
],
[
{
"PP_id":"2",
"PP_purchase":"2",
"PP_imei":"12112",
"PP_purchasePrice":"100",
"PP_salePrice":"200",
"PP_color":"red",
"PP_memory":"8",
"PP_memoryUnit":"GB",
"PP_sim":"Single",
"PP_modifiedBy":null,
"PP_saleStatus":"0",
"PP_saleId":"0",
"User_id":"1",
"deleted":"0"
},
{
"PP_id":"3",
"PP_purchase":"2",
"PP_imei":"78878",
"PP_purchasePrice":"200",
"PP_salePrice":"300",
"PP_color":"blue",
"PP_memory":"4",
"PP_memoryUnit":"MB",
"PP_sim":"Dual",
"PP_modifiedBy":null,
"PP_saleStatus":"1",
"PP_saleId":"1",
"User_id":"1",
"deleted":"0"
}
]
]
如何在ajax中解析此类数据。 这就是我想要的。
[
{
"PP_id":"1",
"PP_purchase":"1",
"PP_imei":"12112",
"PP_purchasePrice":"100",
"PP_salePrice":"200",
"PP_color":"red",
"PP_memory":"8",
"PP_memoryUnit":"MB",
"PP_sim":"Dual",
"PP_modifiedBy":null,
"PP_saleStatus":"1",
"PP_saleId":"1",
"User_id":"1",
"deleted":"0"
},
{
"PP_id":"2",
"PP_purchase":"2",
"PP_imei":"12112",
"PP_purchasePrice":"100",
"PP_salePrice":"200",
"PP_color":"red",
"PP_memory":"8",
"PP_memoryUnit":"GB",
"PP_sim":"Single",
"PP_modifiedBy":null,
"PP_saleStatus":"0",
"PP_saleId":"0",
"User_id":"1",
"deleted":"0"
},
{
"PP_id":"3",
"PP_purchase":"2",
"PP_imei":"78878",
"PP_purchasePrice":"200",
"PP_salePrice":"300",
"PP_color":"blue",
"PP_memory":"4",
"PP_memoryUnit":"MB",
"PP_sim":"Dual",
"PP_modifiedBy":null,
"PP_saleStatus":"1",
"PP_saleId":"1",
"User_id":"1",
"deleted":"0"
}
]
这是我在控制器中的PHP函数,用于加载数据。
public function myControllerMethod()
{
$detail = array();
$product_id = $this->input->post('product_id');
$sale_id = $this->input->post('sale_id');
$purchases=$this->purchase_model->modelFunction($product_id);
$detail=array();
foreach($purchases as $purchase)
{
$detail[] = $this->my_model->mymethod($purchase->$sale_id);
}
echo json_encode($detail);
}
如果PHP循环在控制器中执行一次,则解析工作正常。 我该怎么办。
答案 0 :(得分:0)
$product_id = $this->input->post('product_id');
$sale_id = $this->input->post('sale_id');
$purchases=$this->purchase_model->modelFunction($product_id);
$detail=array();
foreach($purchases as $purchase)
{
$detail[] = $this->my_model->mymethod($purchase->$sale_id);
}
$test_arrey = [];
foreach ($detail as $key => $value) {
foreach ($value as $key => $some) {
$test_arrey[] = $some;
}
}
echo json_encode($test_arrey);
}
希望此代码有效。...