簡體   English   中英

錯誤未定義偏移

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM