php中需要LinkedIn類型的朋友連接 (LinkedIn type friends connection required in php)


問題描述

php中需要LinkedIn類型的朋友連接 (LinkedIn type friends connection required in php)

I am creating a custom social network for one of my clients.

In this I am storing the friends of a user in the form of CSV as shown below in the user table

uid    user_name      friends
1      John           2
2      Jack           3,1
3      Gary           2,4
4      Joey           3

In the above scenario if the logged in user is John and if he visits the profile page of Joey, the connection between them should appear as

John‑>Jack‑>Gary‑>Joey

I am able to establish the connection at level 1 i.e 

If Jack visits Joey's profile I am able to establish the following : 

Jack‑>Gary‑>Joey

But for the 2nd level I need to get into the same routine of for loops which I know is not the right solution + I am not able to implement that as well.

So, can someone please help me with this?

Thanks in Advance, Akash

P:S I am not in a position to change the db architecture :(

‑‑‑‑‑

參考解法

方法 1:

Here's some bfs code I had written in ruby; it should give you a good enough idea of how things work to translate it to php. the other change you'll need to make is to replace graph[current] with a db query to get the current user's friends.

def bfs(graph, start, stop)
  queue = [start]
  visited = {}
  parents = {}
  current = nil
  while true
    if queue.empty?
      return nil
    end
    current = queue.shift
    if current == stop
      return read_path(current, parents)
    end
    visited[current] = true
    graph[current].each do |i|
      if not visited[i] and not queue.index(i)
        parents[i] = current
        queue.push(i)
      end
    end
  end
end

def read_path(node, parents)
  a = [node]
  while parents[node]
    a.push(parents[node])
    node = parents[node]
  end
  return a.reverse
end

GRAPH = {
  "a" => ["b", "c"], 
  "b" => ["c", "d"],
  "c" => ["a", "e"],
  "d" => ["b", "c", "f"],
  "e" => ["c", "f"]
}

path = bfs(GRAPH, "a", "f")
p path

方法 2:

Here's some sample code:

<?php

$currentUID = 1; // The logged in user
$pageUID = 4; // The user whose page is being visited

// Parse the CSV
$csv = explode("\n", $csvData);
$csvlen = count($csv);
for($i=0;$i<$csvlen;$i++) {
    $csv[$i] = explode(",", $csv[$i]);
}

function getFriends($csv, $uid) {
    foreach($csv as $user)
        if($user[0] == $uid)
            return explode(',', $user[2]);
}

$userFriends = getFriends($csv, $currentUID);
$pageFriends = getFriends($csv, $pageUID);

$friendPool = array();
foreach($userFriends as $friend) {
    $hisFriends = getFriends($friend);
    foreach($hisFriends as $subFriend) {
        if(in_array($subFriend, $pageFriends)) {
            if(isset($friendPool[$friend]))
                $friendPool[$friend][] = $subFriend;
            else
                $friendPool[$friend] = array( $subFriend );
        }
    }
}

foreach($friendPool as $friend=>$subFriends)
    foreach($subFriends as $subFriend)
        echo "$currentUID ‑> $friend ‑> $subFriend ‑> $pageUID\n";

(by AkashMartin DeMellomattbasta)

參考文件

  1. LinkedIn type friends connection required in php (CC BY‑SA 3.0/4.0)

#linkedin #CSV #algorithm #PHP






相關問題

php中需要LinkedIn類型的朋友連接 (LinkedIn type friends connection required in php)

我可以在沒有明確用戶批准的情況下獲得 Linkedin oauth 訪問令牌嗎? (Could I get Linkedin oauth access token without explicitly user approve?)

Rails 3 Linkedin API 寶石 (Rails 3 Linkedin API gem)

如何在 iOS SDK 中獲取 LinkedIn 連接/朋友/聯繫人? (How to fetch LinkedIn connections/Friends/Contacts in iOS SDK?)

Linkedin 應用程序具有 OAuth 用戶令牌和 OAuth 用戶密鑰,它們會過期嗎? (Linkedin Application has OAuth User Token and OAuth User Secret, Do they Expire?)

LinkedIn 獲取個人資料提要或新聞提要 2015 (LinkedIn get profile feed or news feed 2015)

OAuth2::LinkedIn 錯誤 (OAuth2::Error with LinkedIn)

如果 Android 中未安裝 LinkedIn 本機應用程序,如何使用 LinkedIn 登錄和發布? (How to login and post with LinkedIn if LinkedIn native app is not installed in android?)

使用 JS API 集成 LinkedIn 登錄 (Integrate LinkedIn login using JS API)

Linkedin oauth2 r_liteprofile 沒有從 api 返回 (Linkedin oauth2 r_liteprofile not being returned from api)

LinkedIn OAuth2.0 使會話無效/強制重新授權 (LinkedIn OAuth2.0 Invalidate Session / Force re-authorization)

LinkedIn API v2 獲取用戶信息 (LinkedIn API v2 get user info)







留言討論