[英]Can't figure out what's wrong with this for loop also array_filter maybe being funny
我有一個從數據庫生成的復選框和文本框表。 當我選中3個復選框中的2個並寫入3個文本框中的2個時。 Checkbox數組有2個元素,textbox數組有3個。我嘗試使用array_filter但它不起作用或其他問題。
$textbox_array=array_filter($_POST['text']);
$checkbox_array = $_POST['check'];
for ($i = 0; $i < count($checkbox_array); $i++) {
$textbox = $textbox_array[$i];
$checkbox = $checkbox_array[$i];
echo $textbox;
echo '-';
echo $checkbox;
}
我選中復選框9和10,並輸入值1和2。
這就是我得到的:1-9-10我應該得到:1-2-9-10
請幫幫我。
問題是$ textbox_array與$ checkbox_array沒有相同的數組鍵。
一種解決方法是重置$ textbox_array的數組鍵。
代替
$textbox_array=array_filter($_POST['text']);
嘗試
$textbox_array=array_values(array_filter($_POST['text']));
您的問題是使用array_filter()
-> Array keys are preserved
。
您需要調用array_values()
以重置數組鍵-> array_values() returns all the values from the array and indexes the array numerically
$textbox_array=array_filter($_POST['text']);
$textbox_array=array_values($textbox_array);
$checkbox_array = $_POST['check'];
for ($i = 0; $i < count($checkbox_array); $i++) {
$textbox = $textbox_array[$i];
$checkbox = $checkbox_array[$i];
echo $textbox;
echo '-';
echo $checkbox;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.