从数组中设置选中的复选框

Set checked checkbox from array

我有 2 组数组,代表所有数据和选定数据

$all_data = ["admin","member","editor"];
$selected = ["admin","member"];

我想制作所有已检查的数据列表 if $all_data value = $selected value 实现它的最佳方法是什么?

试试这个代码 -

<?php
$all_data = ["admin","member","editor"];
$selected = ["admin","member"];

foreach($all_data as $value) {
  $checked = in_array($value, $selected) ? 'checked="checked"' : '';
  echo '<input type="checkbox" name="chk[]" value="' . $value .'" ' . $checked . '>';
}
?>