我有两个下拉菜单,一个是用户列表,另一个是选定的用户列表。我只想列出未选定的用户来列出 我的数组
$this->getusers=
[1] => Array
(
[adminID] => 28
[UserName] => a
[Password] => 345bcc5369bb7408dcfce4a1a97a936cc961499ec696c5161d3d3ffb99e2f179
[salt] => a147b357bd7f351644e784506d87f110
[email] => ar@mjsofttech.com
[profile_id] => 2
[E_id] => 11
[R_head] => 27
)
[2] => Array
(
[adminID] => 27
[UserName] => d
[Password] => d2111a96534b5ac543234ebcd759a2691535ecc43be575aadff463cc4dfbc0cd
[salt] => e9eef7d916e6c9ad5ea64790b8f93a35
[email] => d@mjsofttech.com
[profile_id] => 1
[E_id] => 13
[R_head] => 26
)
[3] => Array
(
[adminID] => 34
[UserName] => f
[Password] => 955cd016646459cb38bd5ba3454724d529a568f2ce0090a3afbc9d50369ddb65
[salt] => 0a6989ec15b6454a24722e8832cd0cd9
[email] => f@mjsofttech.com
[profile_id] => 2
[E_id] => 123
[R_head] => 27
)
[4] => Array
(
[adminID] => 33
[UserName] => j
[Password] => f2852fb272e1ff58c0562096e94d3d1d9b72c6ed7c0c90e67787179918e58153
[salt] => 9a547a397db3f342519c21fb934aeb87
[email] => j@mjsofttech.com
[profile_id] => 3
[E_id] => 12
[R_head] => 29
)
我尝试使用in_array函数进行多次选择
$this->editproject=
Array
(
[0] => Array
(
[Project_name] => test
[p_id] => 1
[gid] => 28,33
)
此处的gid用逗号分隔,
这里28,33
是选定的用户,所以我不想在用户列表中列出28和33
<?php foreach ($this->getusers as $users): ?>
<select name="" id="lstview" class="form-control" size="8" multiple="multiple">
<option value="<?php echo $users['adminID'] ?>"<?php if(!in_array( $this->editproject[0]['gid'],$users['adminID'])){ ?>selected="selected"<?php } ?>><?php echo $users['UserName'] ?></option>
</select>
<?php endforeach ?>
运气不好,我没有得到任何输出。任何帮助将不胜感激
答案 0 :(得分:2)
我认为问题在于您将完整的select元素带入了foreach,您应该将select标记带到了foreach之外
<select name="" id="lstview" class="form-control" size="8" multiple="multiple">
<?php
foreach ($this->getusers as $users) {
$selected = array_column($this->editproject, 'gid');
if (!in_array($users['adminID'], $selected)) {
echo "<option value='" . $users['adminID'] . "' selected>" . $users['UserName'] . "</option>";
}
}
?>
</select>
您使用了错误的in_array
语法。
我认为您可能需要array_column来获取所有gid
并检查in_aray
in_array-检查数组中是否存在值
语法:
in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : bool
答案 1 :(得分:1)
如果您希望跳过选定的ID而不显示在下拉列表中,则可以设置条件以继续循环并获取下一个值。
$selected = $this->editproject[0]['gid'];
foreach ($this->getusers as $users):
if(in_array($users['adminID'], $selected){ continue; }
/** rest of the logic **/
endforeach;