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