我有两个数组,下面是输出。第一个数组是我的所有列表,第二个是由用户选择的。
$specification=getgeneralSpecifications($pdo); // getting below array
Array(
[0] => Array
(
[sp_id] => 1
[specifications_name] => example 1
)
[1] => Array
(
[sp_id] => 2
[specifications_name] => example 2
)
[2] => Array
(
[sp_id] => 3
[specifications_name] => example 3
)
[3] => Array
(
[sp_id] => 4
[specifications_name] => example 4
)
[4] => Array
(
[sp_id] => 5
[specifications_name] => example 5
)
[5] => Array
(
[sp_id] => 6
[specifications_name] => example 6
)
)
$getgeneral = explode(",",$info['developerprofile']['general_Specifications']); // getting below output
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
我必须显示数据库中的所有列表,并且我已经根据获取表单数据库的第二个数组值选中了复选框。
我尝试了下面的代码,我得到了输出,但缺少一个值。我的意思是,如果我有数组1,2,3,4
,那么我会继续1,2,3
<?php
$specification=getgeneralSpecifications($pdo);
$getgeneral = explode(",",$info['developerprofile']['general_Specifications']);
foreach ($specification as $key=> $row) {
$checked="";
if(in_array($key, $getgeneral)){
$checked="checked";
}
?>
<li><label><?php echo $row['specifications_name'];?></label>
<div class="form-check">
<input class="form-check-input custom-checkbox generalsinglecheck" type="checkbox" value="<?php echo $row['sp_id'];?>" name="general_specification[]" <?php echo $checked;?>>
</div>
</li>
<?php } ?>