在這裏,我做的笨,我在這裏有一個數組,這個數組裏面studentAbsentId我存儲在JSON字符串這樣[studentAbsentId] => [「1 」, 「2」],怎樣用笨在數組中的字符串值以JSON
我的控制器
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);
}
我的模型
public function admin_options_listdisplay($params)
{
$this->db->select('*');
$this->db->where('status !=', '1');
$this->db->where('schoolId =', $params['schoolId']);
$this->db->where('classId =', $params['classId']);
$this->db->where('sectionId =', $params['sectionId']);
$this->db->where('studentAbsentDate =', $params['absentDate']);
return $this->db->get('student_absent_list')->result_array();
}
在我的控制器
我打印結果意味着
的var_dump($ absentresponse);
array(1) {
[0]=>
array(9) {
["absendId"]=>
string(1) "1"
["studentAbsentId"]=>
string(9) "["1","2"]"
["studentAbsentDate"]=>
string(10) "2017-04-11"
["schoolId"]=>
string(1) "2"
["classId"]=>
string(1) "1"
["sectionId"]=>
string(1) "1"
["reg_date"]=>
string(19) "2017-04-13 01:01:03"
["created_by"]=>
string(29) "[email protected]"
["status"]=>
string(1) "0"
}
}
我的預期結果
{
"status": 1,
"data":
[
{
"studentabsentId":"1"
"studentabsentName":"Kani"
"studentAbsentDate": "2017-04-12"
},
{
"studentabsentId":"2"
"studentabsentName":"yuvi"
"studentAbsentDate": "2017-04-12"
}
]
}
爲了得到我預期的答案我寫這樣的代碼,
$optionsList = array();
foreach($absentresponse as $hmres)
{
$hmparent['studentabsentId']=json_decode($hmres['studentAbsentId']);
$hmparent['studentAbsentDate']=$hmres['studentAbsentDate'];
array_push($optionsList,$hmparent);
}
$return = array("status" => 1, "data" => $optionsList);
echo json_encode($return);
我正在輸出中
{
"status": 1,
"data": [
{
"studentabsentId": [
"1",
"2"
],
"studentAbsentDate": "2017-04-11"
}
]
}
這不是我期待的結果,請幫助我的人怎麼辦,用於尋找studentAbsentId爲此的名字我已經編寫的功能在幫助我
我的助手
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
}
}
我投票關閉這一問題作爲題外話,因爲StackOverflow的是不是一個自由的人羣來源的代碼生成器。 –