搜索对象数组并返回新的对象数组

我需要一些帮助。我一直在尝试解决这个问题,但我不能,所以我想做的是我试图将对象数组 blackListUser 与contactUsers 进行比较。如果有匹配,我想返回新用户的白名单,而没有后备用户,这是最后一个预期的新数组。


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"

            }


    )


泛舟湖上清波郎朗
浏览 127回答 1
1回答

慕哥6287543

<?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) {&nbsp; &nbsp; return !in_array($user, $blackListedUsers);});var_dump($res);将array_filter与匿名函数一起使用;和in_array。阅读in_array文档以查看它是否符合您的需要,因为它正在比较两个对象,您可以使用一个自定义函数,该函数仅在两个对象的 ID 相等时进行测试。更新或者更适合初学者的代码版本:<?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) {&nbsp; &nbsp; if (!isBlacklisted($user, $blackListedUsers)) {&nbsp; &nbsp; &nbsp; &nbsp; $validUsers[] = $user;&nbsp; &nbsp; }}var_dump($validUsers);function isBlacklisted($user, $blackListedUsers) {&nbsp; &nbsp; foreach ($blackListedUsers as $blackListedUser) {&nbsp; &nbsp; &nbsp; &nbsp; if ($user->user_id == $blackListedUser->user_id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;}
打开App,查看更多内容
随时随地看视频慕课网APP