user3391137
user3391137

Reputation: 441

How to get elements in multi diementional array if one array's elements are not in another array in PHP

I have two arrays in PHP as follows:

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 ));

How do I get the user_id of array in approved_users, which is not available in approval_users ?

In this example, it should return the array

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

Thanks in advance.

Upvotes: 0

Views: 23

Answers (1)

BeetleJuice
BeetleJuice

Reputation: 40936

$results below will contain what you want:

// 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

Upvotes: 1

Related Questions