复选框数组循环问题

时间:2016-09-29 15:49:36

标签: php arrays checkbox

我有5个复选框,如果数组元素与复选框值相同,则我的数组有3个元素,然后选中该复选框,但它应该只打印一次

<?php
$selvals = array("Manhattan", "Bronx", "Brooklyn");
    $myArray = explode(',', $selvals);
    foreach($myArray as $i)
    {

    ?>

    <label class="checkbox-inline"><input type="checkbox" <?php if($i == "Manhattan"){ echo 'checked="checked"';}?> name="manhattan" value="Manhattan" >Manhattan</label>
    <label class="checkbox-inline"><input type="checkbox" <?php if($i == "Bronx"){ echo 'checked="checked"';}?> name="bronx" value="Bronx" >Bronx</label>
    <label class="checkbox-inline"><input type="checkbox" <?php if($i == "Brooklyn"){ echo 'checked="checked"';}?> name="brooklyn" value="Brooklyn" >Brooklyn</label>
    <label class="checkbox-inline"><input type="checkbox" <?php if($i == "Queens"){ echo 'checked="checked"';}?> name="queen" value="Queens" >Queens</label>
    <label class="checkbox-inline"><input type="checkbox" <?php if($i == "Staten Island"){ echo 'checked="checked"';}?> name="staten" value="Staten Island" >Staten Island</label>

    <?php
    }
    ?>

2 个答案:

答案 0 :(得分:1)

我认为你的代码应该是这样的: - :)

upload.py

答案 1 :(得分:0)

explode函数的第二个参数应该是一个字符串。但是您不需要它,只需删除爆炸声明并将$myArray替换为循环中的$selvals

$selvals = array("Manhattan", "Bronx", "Brooklyn");
foreach ($selvals as $i)
         ^^^^^^^