根据数组值检查复选框?

时间:2012-02-15 09:49:39

标签: php arrays checkbox foreach

我目前有两个看起来像这样的数组:

色板:

Array
(
    [0] => Array
        (
            [swatch_id] => 8
            [swatch_file] => orange_swatch.jpg
        )

    [1] => Array
        (
            [swatch_id] => 9
            [swatch_file] => pink_swatch.jpg
        )

    [2] => Array
        (
            [swatch_id] => 10
            [swatch_file] => green_swtach.jpg
        )

)

精选色板:

Array
(
    [0] => Array
        (
            [swatches_has_products_id] => 18
            [swatches_swatch_id] => 8
            [products_product_id] => 19
        )

    [1] => Array
        (
            [swatches_has_products_id] => 19
            [swatches_swatch_id] => 10
            [products_product_id] => 19
        )

)

如果$swatch['swatch_id']等于$selected_swatches['swatches_swatch_id'],我正在尝试选中复选框。我使用以下代码执行此操作:

<?php foreach ($swatches as $k => $swatch): ?>
    <li>
        <img src="<?php echo base_url(); ?>media/images/swatches/<?php echo $swatch['swatch_file']; ?>" height=""/>
        <input type="checkbox" name="product_has_swatch[]" value="<?php echo $swatch['swatch_id']; ?>" <?php    if($swatch['swatch_id'] == $selected_swatches[$k]['swatches_swatch_id']) : ?> checked="checked" <?php endif; ?> />
    </li>
<?php endforeach; ?>

但是如果没有匹配项,我会收到以下错误:

A PHP Error was encountered
   Severity: Notice
    Message: Undefined offset: 2
   Filename: products/create.php
Line Number: 137

第137行是if来检查我是否有匹配项;我哪里错了?

3 个答案:

答案 0 :(得分:0)

因为选定样本数组中没有索引2,这就是为什么它会通知您。

答案 1 :(得分:0)

一个解决方案是:

// make a new array of selected ids
$newArr = array();
foreach($selected_swatches as $val) {
    array_push($newArr, $val['swatches_swatch_id']);
}

// then check with in_array, like:
<?php foreach ($swatches as $k => $swatch): ?>
    <li>
        <img src="<?php echo base_url(); ?>media/images/swatches/<?php echo $swatch['swatch_file']; ?>" height=""/>
        <input type="checkbox" name="product_has_swatch[]" value="<?php echo $swatch['swatch_id']; ?>" <?php if(in_array($swatch['swatch_id'], $newArr)) : ?> checked="checked" <?php endif; ?> />
    </li>
<?php endforeach; >

希望有所帮助

答案 2 :(得分:0)

**Check this out it works fine**

$swatches           = array( 0 =>array('swatch_id'=>8,'swatch_file'=>'orange_swatch.jpg'),
                             1 => array('swatch_id'=>9,'swatch_file'=>'ping_swatch.jpg'),
                             2 =>array('swatch_id'=>10,'swatch_file'=>'green_swatch.jpg')
                           );
$selected_swatches  = array( 0 =>array('swatches_has_products_id'=>18,'swatches_swatch_id'=>8,'products_product_id'=>19),
                             1 =>array('swatches_has_products_id'=>19,'swatches_swatch_id'=>10,'products_product_id'=>19),

                            );               

foreach($swatches as $k=>$swatch) :

    ?>

<li>

 <input type="checkbox" name="product_has_swatch" value="<?php echo $swatch['swatch_id'];?>"

 <?php 
      if($swatch['swatch_id'] == $selected_swatches[$k]['swatches_swatch_id']):
              echo "checked = 'checked'";
      endif;
  ?>


 />
</li>
<?php endforeach; ?>