Php 复选框数组循环问题

Php 复选框数组循环问题,php,arrays,checkbox,Php,Arrays,Checkbox,我有5个复选框,我的数组有3个元素,如果数组元素与复选框值相同,则选中该复选框,但它应该只打印一次 <?php $selvals = array("Manhattan", "Bronx", "Brooklyn"); $myArray = explode(',', $selvals); foreach($myArray as $i) { ?> <label class="checkbox-inline"><input typ

我有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
    }
    ?>

name=“bronx”value=“bronx”>bronx
name=“queen”value=“Queens”>皇后区

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

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

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


name=“bronx”value=“bronx”>bronx
name=“queen”value=“Queens”>皇后区
<?php
$selvals = array("Manhattan", "Bronx", "Brooklyn");
?>
<label class="checkbox-inline"><input type="checkbox" <?php if(in_array("Manhattan",$selvals)){ echo 'checked="checked"';}?> name="manhattan" value="Manhattan" >Manhattan</label>
<label class="checkbox-inline"><input type="checkbox" <?php if(in_array("Bronx",$selvals)){ echo 'checked="checked"';}?> name="bronx" value="Bronx" >Bronx</label>
<label class="checkbox-inline"><input type="checkbox" <?php if(in_array("Brooklyn",$selvals)){ echo 'checked="checked"';}?> name="brooklyn" value="Brooklyn" >Brooklyn</label>
<label class="checkbox-inline"><input type="checkbox" <?php if(in_array("Queens",$selvals)){ echo 'checked="checked"';}?> name="queen" value="Queens" >Queens</label>
<label class="checkbox-inline"><input type="checkbox" <?php if(in_array("Staten Island",$selvals)){ echo 'checked="checked"';}?> name="staten" value="Staten Island" >Staten Island</label>