[英]Error Undefined offset
我想在數據庫表中的行中插入以下復選框,但是有錯誤,如何解決? (我不能在名稱復選框中更改5)
<input type="checkbox" name="checkbox_units_a[5][]" value="tv">
<input type="checkbox" name="checkbox_units_a[5][]" value="radio">
$name_un = $this -> input -> post('name_units_a');
$service_un = $this -> input -> post('checkbox_units_a');
$data3 = array();
foreach($name_un as $idx => $name) {
$data3[] = array(
'name_un' => $name_un[$idx],
'service_un' => json_encode($service_un[$idx]), ); //This is line 210
};
$this -> db -> insert_batch('hotel_units', $data3);
錯誤:
遇到PHP錯誤
嚴重程度:注意
消息:未定義的偏移量:0
文件名:resident.php
行號:210
我在$name_un
中的var_dump
輸出:
array(1) {
[0] = > string(6)"accessories"
}
我在$service_un
中的var_dump
輸出:
array(1) {
[5] = > array(2) {
[0] = > string(15)"tv" [1] = > string(12)"radio"
}
}
錯誤說您的偏移量等於0
可能是您的$idx == 0
和$service_un[$idx] == null
;)
編輯
正如我認為$idx == 0
導致json_encode
未定義偏移量0嘗試重新索引$service_un
數組(如果無法更改HTML中的5
)以使其從0
開始,例如:
$service_un = array_values ( $service_un ); // reindexing array
它看起來像:
array(1) {
[0] => array(2) {
[0] => string(2) "tv"
[1] => string(5) "radio"
}
}
$service_un
只有一個偏移量,它是五(5),因此這里未定義0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.