仅列出自定义用户角色(过滤器阵列)

时间:2015-09-14 13:31:56

标签: user-roles arrays comparison

我使用$available_roles = $wp_roles->get_names()导致以下print_r输出以及所有可用的用户角色:

Array ( 
  [administrator] => Administrator 
  [editor] => Editor 
  [author] => Author 
  [contributor] => Contributor 
  [subscriber] => Subscriber 
  [customer] => Customer 
  [shop_manager] => Shop Manager 
  [custom_role_test1] => Custom Role Test 1 
  [custom_role_test2] => Custom Role Test 2 
)

我已经使用WordPress和WooCommerce中的内置角色定义了第二个数组:

$built_in_roles = array('administrator', 'editor', 'author', 'contributor', 'subscriber',
'shop_manager', 'customer');

PHP中是否有可用于从$built_in_roles数组中删除$available_roles的函数?

这样我得到一个包含以下print_r输出的数组:

Array ( 
  [custom_role_test1] => Custom Role Test 1 
  [custom_role_test2] => Custom Role Test 2
)

1 个答案:

答案 0 :(得分:0)

刚刚找到它:)

`$arr = array( 'element1' => 1, 'element2' => 2, 'element3' => 3, 'element4' => 4 );

$filterOutKeys = array( 'element1', 'element4' );

$filteredArr = array_diff_key( $arr, array_flip( $filterOutKeys ) );`

结果将是这样的:

[' element2'] => 2

[' element3'] => 3

http://php.net/manual/en/function.array-filter.php#87912