比较两个阵列删除重复项
问题描述:
我在用户分配页面上有三个角色(管理员,主管,基本)的建筑管理系统,这将是管理员可以将主要用户分配给主管的清单。比较两个阵列删除重复项
我有两个数组,第一个数组是管理员已被分配到“管理”该特定的基本用户,第二个将只是所有的主管。我的目标是从全部管理员阵列中删除已分配的主管。
//例如
$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);
}
);
+0
非常感谢! –
您确定这是一个PHP代码吗?在PHP中,有一个用于键值赋值的'=>'(而不是':')运算符。 – ksimka
@ksimka哈哈是啊,我写了很多JSON哈哈我会更新它,谢谢。 –