php - in_array() 如何检查两个值 PHP WordPress
问题描述
我正在检查用户的两个角色,但它不起作用。我正在检查它,in_array()
但是当我只传递一个值时它正在工作,但是当我传递两个值时它不起作用。
PHP具有两个用户角色值,它不起作用。代码有什么问题?
$user = wp_get_current_user();
if ( in_array( 'editor','author', (array) $user->roles ) ) {
remove_menu_page('edit.php');
}
}
解决方案
使用 array_intersect() 函数使用大海捞针的目标,并确保交集精确地等于目标:
$user = wp_get_current_user();
$haystack = (array) $user->roles;
$target = array('editor', 'author');
if(count(array_intersect($haystack, $target)) == count($target)){
// all of $target is in $haystack
}
推荐阅读
- react-native - undefined 不是评估 'this.props.dispatch 的函数
- python - 如何在没有 Pub/Sub 的情况下使用 Dataflow 将 Google Cloud Storage 存储桶流式传输到 Big Query
- excel - 无法从 url 中提取 id
- java - 将 jnetpcap 添加到 maven 失败
- google-apps-script - 使用 setValues 函数的问题
- python - 显示所有行时显示行数和列数
- ruby - 如何检测哈希中的重复键并向重复项添加前缀?
- mysql - Codeigniter + MySQL - 搜索包含字符串
- reactjs - null 不是对象(评估“ShareDialog.canShow”)
- jquery - 轮播的暂停问题