我有json文件,我导入到关联数组。这是关于某种社交网络模拟。我列出了一个人员列表,以及一个导致他们个人资料的链接。个人资料包含用户名,年龄,性别以及朋友和朋友的朋友列表。现在我必须在个人资料上列出建议的朋友,但我不知道该怎么做。帮助不大?
推荐朋友:群组中知道所选用户的2个或更多直接朋友但未与所选用户直接关联的人。
<?php
$str = file_get_contents("data.json");
$json = json_decode($str, true);
$i = $_GET['id'];
?>
这是我从json文件导入数据的方式。我使用get方法进行id传输。
{
"id": 2,
"firstName": "Rob",
"surname": "Fitz",
"age": 23,
"gender": "male",
"friends": [
1,
3
]
},
{
"id": 3,
"firstName": "Ben",
"surname": "O'Carolan",
"age": null,
"gender": "male",
"friends": [
2,
4,
5,
7
]
},
{
"id": 4,
"firstName": "Victor",
"surname": "",
"age": 28,
"gender": "male",
"friends": [
3
]
}
这是json的一部分。
<?php
for($m = 0; $m < sizeof($json[$i-1]['friends']); $m++) {
?>
<tr>
<th scope="row"><?php echo $json[$json[$i-1]['friends'][$m] - 1]["id"]; ?></th>
<td><?php echo $json[$json[$i-1]['friends'][$m] - 1]["firstName"]; ?></td>
<td><?php echo $json[$json[$i-1]['friends'][$m] - 1]["surname"]; ?></td>
<td><a href="profile.php?id=<?php echo $json[$json[$i-1]['friends'][$m] - 1]["id"]; ?>">Profile</a></td>
<?php for($j = 0; $j < sizeof($json[$json[$i-1]['friends'][$m] - 1]["friends"]); $j++) { ?>
<td><?php echo $json[$json[$json[$i-1]['friends'][$m] - 1]["friends"][$j] - 1]["firstName"]." ".$json[$json[$json[$i-1]['friends'][$m] - 1]["friends"][$j] - 1]["surname"]; ?></td>
<?php } ?>
</tr>
这就是我如何结交朋友的朋友和朋友
答案 0 :(得分:0)
我拍了一个解决方案。我有2个班,一个代表一个人,另一个代表一个小组。
Person包含他的朋友列表和一些支持您想要询问的查询的函数。
/**
* Represents the group of people
*/
class Group {
// List of everyone
public $personList = [];
// Add a person to the group
function addPerson(Person $person) {
$this->personList[$person->id] = $person;
return $person;
}
// Find suggested friends for everyone
// Return [
// ['id' => 1, 'suggested' => [2,3]],
// ['id' => 2, 'suggested' => [2,3]]
// ]
function suggestedFriendsForEveryone() {
$uggestions = [];
foreach($this->personList as $person) {
$suggestions[] = ['id' => $person->id, 'suggested' => $this->suggestedFriends($person)];
}
return $suggestions;
}
// return list of suggested friends for a person
function suggestedFriends(Person $person) {
// walk through the list and
$a = [];
foreach($this->personList as $p) {
if ( $person->isSuggestedFriend($p) ) {
$a[] = $p->id;
}
}
return $a;
}
// Output list
function showAll() {
foreach($this->personList as $p) {
echo $p->friendsString() . "\n";
}
}
}
class Person {
public $id;
public $friends;
function __construct($id, $friends=[]) {
$this->id = $id;
$this->friends = $friends;
}
/**
* person knows 2 or more of my direct friends but is not my friend
*/
public function isSuggestedFriend(Person $person) {
if ( $person->id == $this->id ) return false; // I am not my own suggested friend
if ( $this->isFriend($person) ) return false;
// Friends that person has that are not my friends
$common = $this->commonFriends($person);
if ( count($common) > 1 ) return true;
return false;
}
// am I a friend of this person
public function isFriend(Person $person) {
if ( $person->id == $this->id ) return false; // I am not my own friend
return in_array($person->id, $this->friends);
}
// returns list of common friends
public function commonFriends(Person $person) {
$diff = array_intersect($person->friends, $this->friends);
return $diff;
}
// Return a string version of friend list
public function friendsString() {
return "Person: {$this->id} [" . implode(', ', $this->friends) . ']';
}
// Return a string version of isSuggestedFriend
public function isSuggestedFriendString(Person $person) {
if ( $this->isSuggestedFriend($person) ) return "Person {$person->id} is a suggested friend for Person {$this->id}";
else "Person {$person->id} is not a suggested friend for Person {$this->id}";
}
}
$g = new Group;
$g->addPerson(new Person(3, [2,4,5]));
$g->addPerson(new Person(4, [1,2,3]));
$g->addPerson(new Person(5, [3]));
$g->addPerson(new Person(6));
$g->addPerson(new Person(7));
$g->addPerson(new Person(1, [2,3,4]));
$g->addPerson(new Person(2, [1,4]));
$g->showAll();
//echo implode(', ', $g->suggestedFriends($p1)) . "\n";
$sl = $g->suggestedFriendsForEveryone();
echo "Suggested Friends\n";
foreach($sl as $row) {
echo "Person {$row['id']} suggested [" . implode(', ', $row['suggested']) . "]\n";
}