2012-08-02 80 views
1

我將字段名存儲在一個數組中,希望能夠動態地創建變量。來自數組的動態變量

我收到一個非法偏移類型錯誤的,如果和其他人,這兩條線:

$data[$tmp_field] = $tmp_field[$id]; 

$data[$tmp_field] = 0; 

我檢查後數據,並將其與適當的數據發佈,但是我不知道是什麼問題是。

$ student_id數據存儲所有學生的ID,例如:$student_id = array(8,9,11,23,30,42,55);

function updateStudentInfo() { 
    $student_id = $this->input->post('student_id'); 
    $internet_student = $this->input->post('internet_student'); 
    $dismissed = $this->input->post('dismissed'); 
    $non_matriculated_student = $this->input->post('non_matriculated_student'); 
    $felony = $this->input->post('felony'); 
    $probation = $this->input->post('probation'); 
    $h_number = $this->input->post('h_number'); 
    $office_direct_to = $this->input->post('office_direct_to'); 
    $holds = $this->input->post('holds'); 

    $fields = array('internet_student', 'non_matriculated_student', 'h_number', 'felony', 'probation', 'dismissed'); 

    foreach($student_id as $id): 
    $data = array(); 

    foreach($fields as $field_name): 
     $tmp_field = ${$field_name}; 

     if(empty($tmp_field[$id])) { 
     $data[$tmp_field] = 0; 
     } else { 
     $data[$tmp_field] = $tmp_field[$id]; 
     } 
    endforeach; 

    print '<pre style="color:#fff;">'; 
    print_r($data); 
    print '</pre>'; 

    endforeach; 
} 

這是陣列格式我的願望:

Array 
(
    [internet_student] => 1 
    [non_matriculated_student] => 1 
    [h_number] => 0 
    [felony] => 0 
    [probation] => 1 
    [dismissed] => 0 
) 

新增截圖給你視覺形式的數據正在發佈從

enter image description here

+0

當您使用$ data [$ field_name]而不是$ data [$ tmp_field]時會發生什麼?您正嘗試將密鑰設置爲數組,這可能會導致錯誤。 – user1190992 2012-08-02 15:02:46

+0

我會走下$ student = array_merge(array('internet_student'=> 0,'non_matriculated_student'=> 0,'h_number'=> 0,'felony'=> 0,'probation'=> 0 ,'dismissed'=> 0),$ student);這將創建一個0值的數組,並超過它的頂部任何現有的值 – Waygood 2012-08-02 15:04:23

+0

@Waygood你能提交你的答案與該例子? – Brad 2012-08-02 15:17:29

回答

1
foreach($student_id as $id): 
    $data = array(); 

    foreach($fields as $field_name): 
     $tmp_field = ${$field_name}; 

     if(empty($tmp_field[$id])) { 
     $data[$field_name] = 0; 
     } else { 
     $data[$field_name] = $tmp_field[$id]; 
     } 
    endforeach; 

    print '<pre style="color:#fff;">'; 
    print_r($data); 
    print '</pre>'; 

    endforeach; 
+0

這看起來確實有效。我有$ data [$ tmp_field] = $ tmp_field [$ id];錯誤。 – Brad 2012-08-02 15:32:16

0

我假設所有這些字段都是數組,否則你不需要任何循環。

function updateStudentInfo() 
{ 
    $student_id     = $this->input->post('student_id'); 
    $internet_student   = $this->input->post('internet_student'); 
    $dismissed    = $this->input->post('dismissed'); 
    $non_matriculated_student = $this->input->post('non_matriculated_student'); 
    $felony      = $this->input->post('felony'); 
    $probation    = $this->input->post('probation'); 
    $h_number     = $this->input->post('h_number'); 
    $office_direct_to   = $this->input->post('office_direct_to'); 
    $holds     = $this->input->post('holds'); 

    $fields = array('internet_student', 'non_matriculated_student', 'h_number', 'felony', 'probation', 'dismissed'); 

    $student_count = count($student_id); 
    foreach($student_id as $id) 
    { 
     $data = array(); 
     foreach($fields as $field) 
     { 
      if(array_key_exists($id, $$field)) 
       $data[$field] = ${$field}[$id]; 
     } 
    } 
} 

您正在嘗試使用學生證作爲數組鍵的其他領域,但HTML形式僅僅是一個標準的索引數組,而不是鍵入任何學生數據。

+0

$ student_id存儲一個學生ID的數組,因此我使用其中的ID來從每個數組中提取適當的數據。例如,$ student_id = array(8,9,11,21,30,43); – Brad 2012-08-02 15:13:25

+0

@Brad是來自HTML表單的其他字段(h_number,保留等)?如果是這樣,那麼他們沒有鍵入學生ID – Patrick 2012-08-02 15:14:25

+0

是的,他們被鍵入到$ student_id ID中。在視圖中,我有這樣的字段名:h_number [$ s-> id],相同的持有[$ s-> id] [],駁回[$ s-> id] – Brad 2012-08-02 15:16:42