【问题标题】:Only list custom user roles (filter array)仅列出自定义用户角色(过滤器数组)
【发布时间】:2016-05-29 13:45:12
【问题描述】:

我使用$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 中是否有可用于从$available_roles 数组中删除$built_in_roles 的函数?

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

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

【问题讨论】:

    标签: user-roles arrays comparison


    【解决方案1】:

    刚刚找到了:)

    `$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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 2014-12-16
      • 2021-11-26
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多