in_array第一次在复选框中工作

时间:2013-04-23 17:29:43

标签: php arrays

一个奇怪的问题。这是带有一些值的数组

$et_er_facilities = ( [0] => Swimming pool 
[1] => Squash court 
[2] => Mini market 
[3] => Playground ) 

如果我添加一些复选框来检查上面数组中的值,我试图使用in_array函数。该功能适用​​于第一次检查,但在所有下一次检查时,它不起作用。这是我的代码。

<input type="checkbox" id="facilities1" value="Swimming pool" name="et_er_facilities[]" <?php if (in_array("Swimming pool", $et_er_facilities)) {?>checked="checked"<?php }?>>
<input type="checkbox" id="facilities4" value="Squash court" name="et_er_facilities[]" <?php if (in_array('Squash court', $et_er_facilities)) {?>checked="checked"<?php }?>>
<input type="checkbox" id="facilities5" value="Mini market" name="et_er_facilities[]" <?php if (in_array('Mini market', $et_er_facilities)) {?>checked="checked"<?php }?>>
<input type="checkbox" id="facilities7" value="Playground" name="et_er_facilities[]" <?php if (in_array('Playground', $et_er_facilities)) {?>checked="checked"<?php }?>>

所以这就是它目前的工作方式。如果在上面的数组中,第一个值是Playground,则复选框将仅选择Playground,而不会自动选择其他值。

在所有情况下,它只匹配数组的第一个值并选中相应的复选框,但如果它们也匹配则不选择其他复选框。

任何帮助,都将受到高度赞赏。

由于

2 个答案:

答案 0 :(得分:2)

将数组初始化为

$et_er_facilities =  array(
    0 => 'Swimming pool',
    1 => 'Squash court', 
    2 => 'Mini market', 
    3 => 'Playground'
);

答案 1 :(得分:0)

我修改了你的代码

<html>
<body>
<?php
*$et_er_facilities = array("Swimming pool","Squash court","Mini market","Playground" );* // most prob this was wrong in ur code

?>
<form>
<input type="checkbox" id="facilities1" value="Swimming pool" name="et_er_facilities[]"         <?php if (in_array("Swimming pool", $et_er_facilities)) {?>checked="checked"<?php }?>>
<input type="checkbox" id="facilities4" value="Squash court" name="et_er_facilities[]" <?php if (in_array('Squash court', $et_er_facilities)) {?>checked="checked"<?php }?>>
<input type="checkbox" id="facilities5" value="Mini market" name="et_er_facilities[]" <?php if (in_array('Mini market', $et_er_facilities)) {?>checked="checked"<?php }?>>
<input type="checkbox" id="facilities7" value="Playground" name="et_er_facilities[]" <?php if (in_array('Playground', $et_er_facilities)) {?>checked="checked"<?php }?>>
</form>
</body>
</html>
  1. 没有数组中的SwimmingPool $ et_er_facilities = array(“壁球场”,“迷你市场”,“游乐场”); 除了第一个

  2. 之外,它还提供了4个复选框
  3. 使用数组游泳池 $ et_er_facilities = array(“游泳池”,“壁球场”,“迷你市场”,“游乐场”); 它检查了所有4个复选框