foreach循环不执行

时间:2015-05-08 08:59:31

标签: php loops foreach

嗨我有一个表格,在复选框中有几个答案

<p>Select the modules you take:<br />
Module1 <input type="checkbox" name="modules" value="Module1"/><br />
Module2 <input type="checkbox" name="modules" value="Module2" /><br />
Module3 <input type="checkbox" name="modules" value="Module3" /><br /></p>

现在在响应页面中我有foreach循环,但它没有执行任何操作

foreach ($modules as $selected){
    print "<p>Modules are ".$selected."</p>";
};

对此有何帮助?提前谢谢

2 个答案:

答案 0 :(得分:1)

如果您将复选框命名为此,则会得到一个数组(带括号[])

<input type="checkbox" name="modules[]" value="Module1"/>

当然你需要迭代正确的数组:

foreach($_POST['modules'] as $selected) {

答案 1 :(得分:0)

您可能知道,var items = new List<MyObject>() { new MyObject() { Id = 1, OperationId = 1}, new MyObject() { Id = 1, OperationId = 2}, new MyObject() { Id = 2, OperationId = 1}, new MyObject() { Id = 2, OperationId = 2}, new MyObject() { Id = 3, OperationId = 2} }; foreach (var obj in items) { Console.WriteLine(obj); } var result = RemoveMaxOpIdInIdGroup(items); Console.WriteLine("Result:"); foreach (var obj in result) { Console.WriteLine(obj); } 循环用于数组 在你的php响应代码中,你应该foreach
$modules=$_POST['modules']循环应如下所示:

foreach

查看THIS文章,以便更熟悉foreach($modules as $selected){ print "<p>Modules are " . $selected . "</p>"; } 循环。