2017-04-14 28 views
-1

這裏我有一個陣列,該陣列中我還有一個陣列稱爲studentabsentId使用功能的json_decode()內,如何使用CI

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); 
} 

回聲json_encode($返程);

{ 
    "status": 1, 
    "data": [ 
    { 
     "studentabsentId": [ 
     "1" 
     ], 
     "studentAbsentDate": "2017-04-12" 
    } 
    ] 
} 

高達現在它的做工精細,現在我想要什麼手段,studentabsentId 1指的是他的名字,假設studentabsentId 2的意思是名稱,爲此我寫所謂getStudentnameById在輔助功能/custom_helper.php。

所以在我的每個環路I增加了一個線這樣

$ hmparent [ 'studentabsentName'] = json_decode(getStudentnameById($ hmres [ 'studentAbsentId']));

但現在我得到未定義的錯誤偏移: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] => [email protected] 
      [status] => 0 
     ) 

) 

預期結果

{ 
"status": 1, 
"data": [ 
{ 
    "studentabsentId":"1" 
    "studentabsentName":"Kani" 
    "studentAbsentDate": "2017-04-12" 
} 
] 
} 
+1

你檢查了json_decode()的輸出; ?如果是,那麼肯定你會有想法。 –

+0

我不能夠找到答案,你可以請更新你的答案 –

+0

@subikshanM只是轉儲json_decode,看看是否返回數組或對象或有一些錯誤 –

回答

0

你getStudentnameById理應返回一個字符串,那麼你爲什麼這樣做最重要的是一個json_decode。 $hmparent['studentabsentName']=json_decode(getStudentnameById($hmres['studentAbsentId']));

還要檢查由該函數的返回。如果你開啓了你的error_reporting來告訴你發生錯誤的行,那麼你應該能夠調試這種代碼,然後var_dump所有與它相關的變量。