简体   繁体   English

两个阵列的Foreach循环?

[英]Foreach loop for two arrays?

I have two arrays. 我有两个数组。 One for input boxes and other for checkbox. 一个用于输入框,另一个用于复选框。

inputbox[] checkbox[] inputbox []复选框[]

inputbox[] checkbox[] . inputbox []复选框[]。 . . . submit button 提交按钮

When I fill check box1 and fill the value in input box1 and try to submit. 当我填写复选框1并填写输入框1中的值并尝试提交。 Foreach fails because it pass all the indexes of input boxes but only passes checked checkbox. Foreach失败,因为它传递了输入框的所有索引,但只传递了复选框。

foreach(array_combine($checkbox, $inputbox) as $check => $input) 

Please tell me what can i do? 请告诉我,我该怎么办?

迭代文本框(保证全部存在),然后获取相应的复选框(可能是ID,如果你之间有某种ID对应 - 你应该这样)。

if you have a control over the HTML form you can make the form in following manner 如果您可以控制HTML表单,则可以按以下方式创建表单

<input type="text" name="name[1]" />
<input type="checkbox" name="check[1]" />
<input type="text" name="name[2]" /> 
<input type="checkbox" name="check[2]" /> 
<input type="text" name="name[3]" />
<input type="checkbox" name="check[3]" />
<input type="text" name="name[4]" />
<input type="checkbox" name="check[4]" />

in that case you will get the post array in the following manner 在这种情况下,您将以下列方式获取post数组

Array
(
    [name] => Array
        (
            [1] => Swapnil
            [2] => 
            [3] => Sarwe
            [4] => Swapnil Sarwe
        )

    [check] => Array
        (
            [1] => on
            [3] => on
        )

)

Now you can loop over the name(input box) and then check isset for the isset($_POST['check'][$key]) and set the default value 现在你可以遍历名称(输入框),然后检查isset是否为isset($ _ POST ['check'] [$ key])并设置默认值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM