0

I need some help with this. i have been trying to solve this out but i can not so what i am trying to do is I am trying to compare to array of objects blackListUser with contactUsers. if there is match i want to return white list of new users without the backlist users which is the last expected new array.

for($counter = 0; $counter < count($blackListUsers); $counter++) {
    how i can check here
}


ContactUsers
Array
(
    [0] => stdClass Object
        (
            [user_id] => 2
            [full_name] => User B
            [image] => 1582735876user-a.png
        )

    [1] => stdClass Object
        (
            [user_id] => 3
            [full_name] => User C
            [image] => 1582735876user-a.png
        )

    [2] => stdClass Object
        (
            [user_id] => 4
            [full_name] => User D
            [image] => 1582735876user-a.png
        )

    [3] => stdClass Object
        (
            [user_id] => 5
            [full_name] => User E
            [image] => 1582735876user-a.png
        )

    [4] => stdClass Object
        (
            [user_id] => 6
            [full_name] => User F
            [image] => 1582735876user-a.png
        )

    [5] => stdClass Object
        (
            [user_id] => 8
            [full_name] => User G
            [image] => 1582735876user-a.png
        )

)


blackListUsers
Array
(
    [0] => stdClass Object
        (
            [user_id] => 2
            [full_name] => User B
            [image] => 1582735876user-a.png
        )

    [1] => stdClass Object
        (
            [user_id] => 3
            [full_name] => User C
            [image] => 1582735876user-a.png
        )

)

expected new array
 Array
    (
        {
                "user_id": "4",
                "name": "User D",
                "image": "1582735876user-a.png"
            },
            {
                "user_id": "5",
                "name": "User E",
                "image": "1582735876user-a.png"
            },
            {
                "user_id": "6",
                "name": "User F",
                "image": "1582735876user-a.png"
            },
            {
                "user_id": "8",
                "name": "User G",
                "image": "1582735876user-a.png"
            }

    )
3
  • Why are you using for instead of foreach? Commented Mar 22, 2020 at 21:16
  • Use array_filter(). The test function checks whether $element->user_id is in the blacklist. Commented Mar 22, 2020 at 21:17
  • Can you help with some code Commented Mar 22, 2020 at 21:21

1 Answer 1

1
<?php
$user1->user_id = 2;
$user1->full_name = 'User B';
$user1->image = '1582735876user-a.png';

$user2->user_id = 3;
$user2->full_name = 'User C';
$user2->image = '1582735876user-a.png';

$user3->user_id = 4;
$user3->full_name = 'User D';
$user3->image = '1582735876user-a.png';

$user4->user_id = 3;
$user4->full_name = 'User C';
$user4->image = '1582735876user-a.png';

$users = [$user1, $user2, $user3];
$blackListedUsers = [$user4];

$res = array_filter($users, function ($user) use ($blackListedUsers) {
    return !in_array($user, $blackListedUsers);
});

var_dump($res);

Using array_filter with an anonymous function ; and in_array. Read the in_array doc to see if it fits your need, since it is comparing two objects, you may use a custom function which tests only if the ids of the two objects are equal.

UPDATE

Or a version of the code which is more beginner friendly:

<?php
$user1->user_id = 2;
$user1->full_name = 'User B';
$user1->image = '1582735876user-a.png';

$user2->user_id = 3;
$user2->full_name = 'User C';
$user2->image = '1582735876user-a.png';

$user3->user_id = 4;
$user3->full_name = 'User D';
$user3->image = '1582735876user-a.png';

$user4->user_id = 3;
$user4->full_name = 'User C';
$user4->image = '1582735876user-a.png';

$users = [$user1, $user2, $user3];
$blackListedUsers = [$user4];

$validUsers = [];
foreach ($users as $user) {
    if (!isBlacklisted($user, $blackListedUsers)) {
        $validUsers[] = $user;
    }
}

var_dump($validUsers);

function isBlacklisted($user, $blackListedUsers) {
    foreach ($blackListedUsers as $blackListedUser) {
        if ($user->user_id == $blackListedUser->user_id) {
            return true;
        }
    }

    return false;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Or just: $res = array_diff($users, $blackListedUsers);
Hi, @Jeto awesome proposition, but when I tested it i got this error: "Catchable fatal error: Object of class stdClass could not be converted to string". Checking the doc it says that "Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In other words: when the string representation is the same.". So one need to add a __toString() for the User object. You can do it and put your response it seems good to me too.
Oh, that's right, never mind then. Still no idea why array_diff doesn't just use the equality operator behind the scenes, like in_array does.

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.