Skip to content
Advertisement

Search an array of object and return new array of objects

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"
            }

    )

Advertisement

Answer

<?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;
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement