2015-09-14 92 views
0

我用​​這將導致以下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 
) 

回答

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