Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否基于数组值选中复选框?_Php_Arrays_Checkbox_Foreach - Fatal编程技术网

Php 是否基于数组值选中复选框?

Php 是否基于数组值选中复选框?,php,arrays,checkbox,foreach,Php,Arrays,Checkbox,Foreach,我目前有两个阵列,如下所示: 样例: Array ( [0] => Array ( [swatch_id] => 8 [swatch_file] => orange_swatch.jpg ) [1] => Array ( [swatch_id] => 9 [swatch_file] => pink_s

我目前有两个阵列,如下所示:

样例:

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
        )

)
选定样例:

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\u id']
等于
$selected\u swatches['swatches\u swatch\u 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; ?>

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

因为在选定的样例中没有索引2。。这就是为什么它会通知您。

一个解决方案是:

// 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; >
//创建所选ID的新数组
$newArr=array();
foreach($val为选定的样本){
数组推送($newArr,$val['swatches\u swatch\u id']);
}
//然后检查in_数组,如:
  • 媒体/图像/样本/“高度=”“/>
    **看看这个,很好用**
    $swatch=array(0=>array('swatch\u id'=>8,'swatch\u file'=>'orange\u-swatch.jpg'),
    1=>array('swatch\u id'=>9,'swatch\u file'=>ping\u swatch.jpg'),
    2=>array('swatch\u id'=>10,'swatch\u file'=>'green\u swatch.jpg')
    );
    $selected_-swatches=array(0=>array('swatches_有_-products_-id'=>18,'swatches_-swatch-id'=>8,'products_-products_-id'=>19),
    1=>array('swatches\u拥有产品\u id'=>19,'swatches\u-swatch\u id'=>10,'products\u-product\u id'=>19),
    );               
    foreach($k=>$swatch的样例):
    ?>
    
  • **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; ?>