如果数组1中的数据等于数组2,PHP将复选框设置为选中

如果数组1中的数据等于数组2,PHP将复选框设置为选中,php,mysql,arrays,checkbox,Php,Mysql,Arrays,Checkbox,我有一个从mysql检索的数组 数组item\u category是一个显示所有类别的数组 $item_category[32]="western food"; $item_category[33]="chinese food"; $item_category[34]="mix food"; $item_category[35]="japanese food"; $item_category[36]="korean food"; $item_category[37]="

我有一个从mysql检索的数组

数组
item\u category
是一个显示所有类别的数组

$item_category[32]="western food";  
$item_category[33]="chinese food";  
$item_category[34]="mix food";     
$item_category[35]="japanese food"; 
$item_category[36]="korean food";   
$item_category[37]="italian food";  

$selected_category[32]="western food"; 
$selected_category[33]="chinese food";  
$selected_category[34]="mix food";    

foreach ($item_category as $key => $value) {

    echo '<input type="checkbox" name="check_list[] "value="'.$key.'"> '.$value.'<br>';

}
数组
selected\u category
是一个显示所选类别的数组

$item_category[32]="western food";  
$item_category[33]="chinese food";  
$item_category[34]="mix food";     
$item_category[35]="japanese food"; 
$item_category[36]="korean food";   
$item_category[37]="italian food";  

$selected_category[32]="western food"; 
$selected_category[33]="chinese food";  
$selected_category[34]="mix food";    

foreach ($item_category as $key => $value) {

    echo '<input type="checkbox" name="check_list[] "value="'.$key.'"> '.$value.'<br>';

}

您需要检查
$item\u category
$key
是否出现在
$selected\u category
中。这可以通过
isset
功能完成:

foreach ($item_category as $key => $value) {
    // check if `$key` set in `$selected_category` 
    $checked = isset($selected_category[$key])? 'checked' : '';

    echo '<input type="checkbox" ' . $checked .' name="check_list[] "value="'.$key.'"> '.$value.'<br>';
}
foreach($key=>value的项目类别){
//检查是否在“$selected\u category”中设置了“$key”
$checked=isset($selected_category[$key])?“checked”:“;
回显'.$value'
'; }
您需要检查
$item\u category
$key
是否出现在
$selected\u category
中。这可以通过
isset
功能完成:

foreach ($item_category as $key => $value) {
    // check if `$key` set in `$selected_category` 
    $checked = isset($selected_category[$key])? 'checked' : '';

    echo '<input type="checkbox" ' . $checked .' name="check_list[] "value="'.$key.'"> '.$value.'<br>';
}
foreach($key=>value的项目类别){
//检查是否在“$selected\u category”中设置了“$key”
$checked=isset($selected_category[$key])?“checked”:“;
回显'.$value'
'; }
试试这个:

$item_category[32]="western food";  
$item_category[33]="chinese food";  
$item_category[34]="mix food";     
$item_category[35]="japanese food"; 
$item_category[36]="korean food";   
$item_category[37]="italian food";  

$selected_category[32]="western food"; 
$selected_category[33]="chinese food";  
$selected_category[34]="mix food";    

foreach ($item_category as $key => $value)
{

    echo '<input type="checkbox" name="check_list[] "value="' . $key . '" ' . ((in_array($value), $selected_category) ? 'checked="checked"' : '') . '>    '.$value.'<br>';

}
$item_category[32]=“西餐”;
$item_category[33]=“中餐”;
$item_category[34]=“混合食品”;
$item_category[35]=“日本食品”;
$item_category[36]=“韩国食品”;
$item_category[37]=“意大利食品”;
$selected_category[32]=“西餐”;
$selected_category[33]=“中餐”;
$selected_category[34]=“混合食品”;
foreach($key=>$value的项目\类别)
{
回显'.$value'
'; }
如果一个指定数组中存在一个指定值,则返回true。请尝试以下操作:

$item_category[32]="western food";  
$item_category[33]="chinese food";  
$item_category[34]="mix food";     
$item_category[35]="japanese food"; 
$item_category[36]="korean food";   
$item_category[37]="italian food";  

$selected_category[32]="western food"; 
$selected_category[33]="chinese food";  
$selected_category[34]="mix food";    

foreach ($item_category as $key => $value)
{

    echo '<input type="checkbox" name="check_list[] "value="' . $key . '" ' . ((in_array($value), $selected_category) ? 'checked="checked"' : '') . '>    '.$value.'<br>';

}
$item_category[32]=“西餐”;
$item_category[33]=“中餐”;
$item_category[34]=“混合食品”;
$item_category[35]=“日本食品”;
$item_category[36]=“韩国食品”;
$item_category[37]=“意大利食品”;
$selected_category[32]=“西餐”;
$selected_category[33]=“中餐”;
$selected_category[34]=“混合食品”;
foreach($key=>$value的项目\类别)
{
回显'.$value'
'; }

如果一个指定数组中存在一个指定值,则返回true

请尝试下面的代码,因为它适合您

<?php
$item_category[32]="western food";  
$item_category[33]="chinese food";  
$item_category[34]="mix food";     
$item_category[35]="japanese food"; 
$item_category[36]="korean food";   
$item_category[37]="italian food";  

$selected_category[32]="western food"; 
$selected_category[33]="chinese food";  
$selected_category[34]="mix food";    

foreach ($item_category as $key => $value) {
      echo '<input type="checkbox" '.(isset($selected_category[$key])? 'checked' : '').' name="check_list[] "value="'.$key.'"> '.$value.'<br>';

}

试试下面的代码,它适合你

<?php
$item_category[32]="western food";  
$item_category[33]="chinese food";  
$item_category[34]="mix food";     
$item_category[35]="japanese food"; 
$item_category[36]="korean food";   
$item_category[37]="italian food";  

$selected_category[32]="western food"; 
$selected_category[33]="chinese food";  
$selected_category[34]="mix food";    

foreach ($item_category as $key => $value) {
      echo '<input type="checkbox" '.(isset($selected_category[$key])? 'checked' : '').' name="check_list[] "value="'.$key.'"> '.$value.'<br>';

}