如何显示数据库中的 selected 值并显示在 php 中的多个 select 框中

How to show the selected values from database and show in multiple select box in php

我有一个像

这样的数组

$a = array("A", "B", "C");

我已经使用多个 select.

将值存储在数据库中

保存的值 "B", "C" 在 2 个不同的行中

我需要在同一个 select 框中显示 selected 值 "b""D" 值。

这里的困难概念是我需要显示数组中 select 选项的值,如下所示

   foreach($dbRows as $dbRow) {
       // here if i display the selected values using if condition the value are selected by the array values repeats like
        a - no selected
        b - selected
        c - no selected

again loops repeats like

       a - no selected
       b - no selected
       c - selected

   }
}

如何在不重复的情况下显示值?

您可以循环 select 框数组并使用 in_array()

检查它的值是否在 DB 数组中
$a = array("A", "B", "C");

foreach($a as $v)
{
    $selected = in_array($v, $db_array) ? 'selected' : '';
}