我必须将dropdown与多个数组进行比较,我的第一个数组是
[menu_master] => Array
(
[master/city] => City
[master/national_holiday] => National Holiday
[master/operator_comments] => Operator Comments
[master/sensors] => Sensors
[master/modbus] => Modbus
[master/manufacturers] => Manufacturers
[master/make_model] => Make Model
)
我的第二个数组是
[role_assign] => Array
(
[1] => View
[2] => Write
)
我的第三个阵列来自数据库
[database_fetch] => Array
(
[master/city] => 1
[master/national_holiday] => 2
[master/operator_comments] => 1
[master/sensors] => 2
[master/modbus] => 1
[master/manufacturers] => 2
[master/make_model] => 1
)
现在这些数组适用于下面的代码。 我想显示选择的下拉选项,它来自数据库获取数组。
<?php
$menu_manster = menu_master();
$i = 1;
foreach($menu_manster as $k => $val) {
?>
<tr>
<td>{{ $i }}</td>
<td class="mailbox-name">{{ $val }}</td>
<td><?php $roles_assign = roles_assign_id(); ?>
<select class="form-control master-menu" name="master_menu[{{$k}}]">
<option value="">Select Role</option>
<?php
foreach ($roles_assign as $key => $value) {
?>
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php } ?>
</select>
</td>
</tr>
<?php $i++; } ?>
答案 0 :(得分:1)
在$roles_assign
循环中,您必须检查$value
是否与database_fetch
项的值匹配。假设您已将database_fetch数据存储在名为$database_fetch
的变量中,解决方案将如下所示:
<?php
$menu_manster = menu_master();
$i = 1;
foreach($menu_manster as $k => $val) {
?>
<tr>
<td>{{ $i }}</td>
<td class="mailbox-name">{{ $val }}</td>
<td><?php $roles_assign = roles_assign_id(); ?>
<select class="form-control master-menu" name="master_menu[{{$k}}]">
<option value="">Select Role</option>
<?php
foreach ($roles_assign as $key => $value) {
?>
<option value="<?php echo $key; ?>"<?=($database_fetch[$k] === $key ? ' selected' : '')?>><?php echo $value; ?></option>
<?php } ?>
</select>
</td>
</tr>
<?php $i++; } ?>