如果一个数组的元素不在PHP的另一个数组中,如何在多个diementional数组中获取元素

时间:2016-07-21 01:41:08

标签: php arrays cakephp

我在PHP中有两个数组如下:

approval_users:

Array ( [0] => Array ( [user_id] => 2 [username] => aamir [remark] => as
approved [levels] => 1 [status] => Approved ) [1] => Array ( [user_id] =>
[username] => easy [remark] => [levels] => 2 [status] => - ));

approved_users

Array ( [0] => Array ( [user_id] => 2 [username] => aamir [remark] => as
approved [status] => Approved ) [1] => Array ( [user_id] => 3 [username] =>
demo [remark] => as approved [status] => Approved ));

如何在 approved_users 中获取 approved_users 中的数组的user_id?

在这个例子中,它应该返回数组

[1] => Array ( [user_id] => 3 [username] =>
demo [remark] => as approved [status] => Approved ).

提前致谢。

1 个答案:

答案 0 :(得分:1)

下面的

$results将包含您想要的内容:

// list of all IDs in $approval_users
$approval_users_ids = array_column($approval_users, 'user_id');
$results = [];

foreach($approved_users as $user){
    //if current approved_user is not found in the list, add it to results
    if(!in_array($user['user_id'],$approval_users_ids)) $results[]=$user;
}

Live demo