这里我有一个数组,在这个数组中我还有一个名为 studentabsentId 的数组,
public function admin_getabsentlist()
{
$data = array(
"schoolId" => $_POST['schoolName'],
"classId" => $_POST['className'],
"sectionId" => $_POST['sectionName'],
"absentDate" => date("Y-m-d", strtotime($_POST['absentDate']))
);
$absentresponse= $this->Admin_attendance_model->admin_options_listdisplay($data);
$optionsList = array();
foreach($absentresponse as $hmres)
{
$hmparent['studentabsentId']=json_decode($hmres['studentAbsentId']);
$hmparent['studentAbsentDate']=$hmres['studentAbsentDate'];
array_push($optionsList,$hmparent);
}
//echo $json = json_encode($optionsList);
$return = array("status" => 1, "data" => $optionsList);
echo json_encode($return);
}
echo json_encode($ return);
{
"status": 1,
"data": [
{
"studentabsentId": [
"1"
],
"studentAbsentDate": "2017-04-12"
}
]
}
到目前为止工作正常,现在我想要的意思是,studentabsentId 1意味着他的名字是什么,假设studentabsentId 2意味着什么是名字,为此目的我在helper /中编写了名为 getStudentnameById 的函数custom_helper.php。
所以在我的每个循环中,我添加了一行这样的
$ hmparent [' studentabsentName'] = json_decode(getStudentnameById($ hmres [' studentAbsentId']));
但是现在我得到了Undefined offset的错误:0,如何解决这个错误,以及如何显示名称
custom_helper.php
if ( ! function_exists('getStudentnameById')){
function getStudentnameById($id){
$ci =& get_instance();
$ci->load->database();
$ci->db->select('firstName');
$ci->db->where('student_id', $id);
$q = $ci->db->get('new_student')->result();
return $q[0]->firstName; // particulart filed
}
}
我的更新代码
的print_r($ absentresponse);
Array
(
[0] => Array
(
[absendId] => 2
[studentAbsentId] => ["1"]
[studentAbsentDate] => 2017-04-12
[schoolId] => 2
[classId] => 1
[sectionId] => 1
[reg_date] => 2017-04-13 01:01:14
[created_by] => kanniyappan@g2evolution.co.in
[status] => 0
)
)
预期结果
{
"status": 1,
"data": [
{
"studentabsentId":"1"
"studentabsentName":"Kani"
"studentAbsentDate": "2017-04-12"
}
]
}
答案 0 :(得分:0)
你的getStudentnameById可能会返回一个字符串,那你为什么要在它上面做一个json_decode呢? $hmparent['studentabsentName']=json_decode(getStudentnameById($hmres['studentAbsentId']));
同时检查该功能的返回。如果你打开了error_reporting来告诉你发生错误的行,那么你应该能够调试这种代码,然后var_dump所有与之相关的变量。