我現在有兩個數組看起來像這樣的:根據數組值檢查複選框?
色板:
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
檢查我是否有匹配;我哪裏錯了?