簡體   English   中英

比較兩個數組刪除重復項

[英]Compare two arrays remove duplicates

我正在用戶分配頁面上構建具有三個角色(管理員,主管,基本)的管理系統,這將是一個清單,管理員可以在其中將基本用戶分配給主管。

我有兩個數組,第一個數組是已分配給“管理”該特定基本用戶的主管,第二個數組就是所有主管。 我的目標是從所有主管陣列中刪除已經分配的主管。

//例

$supervisors['all'] = [
         ['id'=>'1','first'=>'john','last'=>'doe'],
         ['id'=>'2','first'=>'jane','last'=>'doe']
];
$supervisors['assigned'] = [
         ['id'=>'2','first'=>'jane','last'=>'doe']
];

//所以我要尋找的結果是

$supervisors['all'] = [
         ['id'=>'1','first'=>'john','last'=>'doe'],
];
$supervisors['assigned'] = [
         ['id'=>'2','first'=>'jane','last'=>'doe']
];

嘗試使用array_filter

// here we collect all the "assigned" ids in a plain array
$assignedSvsIds = array_column($supervisors['assigned'], 'id');
// then use it in a filter function
$supervisors['all'] = array_filter(
    $supervisors['all'], 
    function($sv) use ($assignedSvsIds) {
        // if id is in 'assigned', it'll be filtered out, otherwise - kept
        return !in_array($sv['id'], $assignedSvsIds, true);
    }
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM