0

I have 2 array in php:

$all_users = [
    0=[
        user_id = 1,
        team_id = 1,
    ],
    1=[
        user_id = 1,
        team_id = 2,
    ],
    2=[
        user_id = 2,
        team_id = 1,
    ],
    3=[
        user_id = 3,
        team_id = 1,
    ],
];
$selected_users = [
    0=[
        user_id = 1,
        team_id = 1,
    ],
    1=[
        user_id = 3,
        team_id = 1,
    ],
];

I want to return not-selected users. I mean that I want:

$not_selected_users = [
    0=[
        user_id = 1,
        team_id = 2,
    ],
    1=[
        user_id = 2,
        team_id = 1,
    ],
]; 

How I can get this?

It is a multidimensional array with multi conditions. I test with multiple for, array_search, array_keys but I can't get it.

Notice: In all_users user_id can be repeatable and for example each user can be in multi teams or other... I mean we can have repeated user_id in all_users array.

2
  • Have you tried anything? You can do this by comparing values from one array with values of another. Commented Jul 4, 2020 at 17:29
  • What have you tried to achieve this? Stack-over flow is not a free coding service you need to provide us your efforts to achieve this or if you are stuck at something. Please do your own research or provide a minimal example of your work. Click here to know how much research effort is expected of Stack Overflow users. Click here for tips on how to ask good questions. Click here to know what types of questions to avoid asking. Commented Jul 4, 2020 at 17:31

1 Answer 1

2
// To speed up search you need `user_id` values as keys:
$selected_users_keys = array_column($selected_users, 'user_id', 'user_id');
// next just filter:
$not_selected_users = array_filter(
    $all_users,
    function ($v) use ($selected_users_keys) { 
        return !isset($selected_users_keys[$v['user_id']]); 
    }
);

The fiddle.

Modified algorithm fiddle:

$selected_users_keys = [];
foreach ($selected_users as $user) {
    $selected_users_keys[$user['user_id'] . '-' . $user['team_id']] = 1;
}

// next just filter:
$not_selected_users = array_filter(
    $all_users,
    function ($v) use ($selected_users_keys) { 
        return !isset($selected_users_keys[$v['user_id'] . '-' . $v['team_id']]); 
    }
);

print_r($not_selected_users);
Sign up to request clarification or add additional context in comments.

6 Comments

sorry, I update my question. I have a team_id too. in all_users user_id is not unique and it can be repeat.
So modify algorithm so as key become something like user_id:team_id and filter.
thanks but team_id is an example that you can understand my concept and my problem. we should only use user_id. and user_id can be repeat in other arrays properties. my problem is complex and I simplify it by this simple example.
for example It possible that a user not assigened to any team! then he don't have team_id
I'm not going to solve all edge cases of your problem. I showed you a direction. You either follow it and modify the answer to fit your needs or not follow. Over.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.