复选框检查是否为in_array

时间:2014-10-24 17:18:52

标签: php arrays

为什么此代码只检查第一个复选框?

$activetheme = $theme->selectActiveThemeName();
foreach($blocks->availableBlockAreas($activetheme) as $block_name => $block_area_number) {
    $selected = (in_array($block_area_number, $block_areas)) ? ' checked="checked"' : '';
    echo '<p><input class="block_checkboxes" '. $selected .' type="checkbox" name="block_areas[]" value="'.$block_area_number.'" />'.ucfirst($block_name).'</p>';
}

$block_areas是一个数组:

Array ( [0] => 1,2,3 )

$blocks->availableBlockAreas($activetheme)返回

Array ( [Homepage - Left Column] => 1 [Homepage - Right Column] => 2 [Custom Page - Left Column] => 3 [Custom Page - Right Column] => 4 )

$block_area_number应该迭代吗?

修改

在Geoandri'回答之后我想出了这个修复,首先我serialized array插入DB然后:

$block_areas[] = unserialize($row['block_areas']);

$selected = (in_array($block_area_number, $view->array_flatten($block_areas))) ? ' checked="checked"' : '';

1 个答案:

答案 0 :(得分:1)

尝试将$ block_areas更改为

$block_areas = array(1,2,3);

我认为这种方式可行。