我有两个下拉列表,一个是用户列表,另一个是选择userlist.i想要列出非选定用户列出我的阵列
$this->getusers=
[1] => Array
(
[adminID] => 28
[UserName] => a
[Password] => 345bcc5369bb7408dcfce4a1a97a936cc961499ec696c5161d3d3ffb99e2f179
[salt] => a147b357bd7f351644e784506d87f110
[email] => [email protected]
[profile_id] => 2
[E_id] => 11
[R_head] => 27
)
[2] => Array
(
[adminID] => 27
[UserName] => d
[Password] => d2111a96534b5ac543234ebcd759a2691535ecc43be575aadff463cc4dfbc0cd
[salt] => e9eef7d916e6c9ad5ea64790b8f93a35
[email] => [email protected]
[profile_id] => 1
[E_id] => 13
[R_head] => 26
)
[3] => Array
(
[adminID] => 34
[UserName] => f
[Password] => 955cd016646459cb38bd5ba3454724d529a568f2ce0090a3afbc9d50369ddb65
[salt] => 0a6989ec15b6454a24722e8832cd0cd9
[email] => [email protected]
[profile_id] => 2
[E_id] => 123
[R_head] => 27
)
[4] => Array
(
[adminID] => 33
[UserName] => j
[Password] => f2852fb272e1ff58c0562096e94d3d1d9b72c6ed7c0c90e67787179918e58153
[salt] => 9a547a397db3f342519c21fb934aeb87
[email] => [email protected]
[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 ?>
运气不好我没有得到任何输出。任何帮助将非常感激
我认为问题是你在foreach中使用了完整的select元素,你应该在foreach之外选择select标签
<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
s并检查in_aray
in_array - 检查数组中是否存在值
句法:
in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : bool
如果你想跳过所选的id不会出现在下拉列表中,你可以设置一个条件来继续循环并获取下一个值。
$selected = $this->editproject[0]['gid'];
foreach ($this->getusers as $users):
if(in_array($users['adminID'], $selected){ continue; }
/** rest of the logic **/
endforeach;