我將字段名存儲在一個數組中,希望能夠動態地創建變量。來自數組的動態變量
我收到一個非法偏移類型錯誤的,如果和其他人,這兩條線:
$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
)
新增截圖給你視覺形式的數據正在發佈從
當您使用$ data [$ field_name]而不是$ data [$ tmp_field]時會發生什麼?您正嘗試將密鑰設置爲數組,這可能會導致錯誤。 – user1190992 2012-08-02 15:02:46
我會走下$ 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
@Waygood你能提交你的答案與該例子? – Brad 2012-08-02 15:17:29