简体   繁体   English

如何在调用获取关注者/关注 api 时将相互关注者/关注者放在数组顶部?

[英]How to put mutual followers/followings at top of the array when calling get followers/following api?

I'm calling a get API for a list of followers of another user, I want to put the users who I already follow on top of the list.我正在调用 get API 以获得另一个用户的关注者列表,我想将我已经关注的用户放在列表的顶部。 How can I do that efficiently in laravel我怎样才能在 laravel 中有效地做到这一点

this is how I have done it,这就是我的做法,

$user_details = User::with('followings:name,username,about_me,photo_url,leader_id as id')->where('id', $userId)->select('id')->first();
    
$my_details = User::with('followings:leader_id as id')->where('id', $request->logged_in_user->id)->select('id')->first()->toArray();
            $ids = array_column($my_details['followings'], 'id');

            $sortedfollowings = [];
            foreach ($followings as $key => $value) {
                if(in_array($value->id, $ids)){
                    array_unshift($sortedfollowings, $value);
                } else {
                    array_push($sortedfollowings, $value);
                }
            }

I'm sorting list using foreach and if else, but it's not efficient when the list is like million followers.我正在使用 foreach 和 if else 对列表进行排序,但是当列表像百万追随者时效率不高。

This is more of an algorithm type of issue.这更像是一种算法类型的问题。 This also depends on if you're using a package or not.这也取决于您是否使用 package。 Being this said, and not that much information provided and/or examples of what you've tried, I'll proceed explaining the process:话虽如此,并没有提供太多信息和/或您尝试过的示例,我将继续解释该过程:

1.- A foreach loop that checks through your followings. 1.- 检查您的追随者的foreach循环。

2.- Inside that loop, you add a conditional ( if ) that checks if any of your followings follow the id of the user profile you're visiting. 2.- 在该循环中,您添加一个条件 ( if ) 来检查您的任何关注是否遵循您正在访问的用户配置文件的id

3.- Add into an array the users that follow that account. 3.- 将关注该帐户的用户添加到数组中。

4.- Send that array separately to your front-end. 4.- 将该数组单独发送到您的前端。

5.- Merge that array in the beginning of the whole fetch. 5.- 在整个提取开始时合并该数组。

6.- Exclude duplicates. 6.- 排除重复项。

Again, I don't know which front-end you're using.同样,我不知道您使用的是哪个前端。 I don't have any sample code, so I just proceeded with an explanation of how it can work.我没有任何示例代码,所以我只是继续解释它是如何工作的。

This is a matter of loops and array manipulation.这是循环和数组操作的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM