I have json file that i import into associative array. It is about some sort of social network simulation. I made a list of people, and a links which leads to their profile. Profiles contain user name, age, gender and a list of their friends and friends of friends. Now i have to make a list of suggested friends on the profile, but i have no idea how to do it. Little help?
Suggested friends: people in the group who know 2 or more direct friends of the chosen user but are not directly connected to the chosen user.
<?php
$str = file_get_contents("data.json");
$json = json_decode($str, true);
$i = $_GET['id'];
?>
This is how i import data from json file. I used get method for id transfer.
{
"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
]
}
This is a part of 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>
This is how i got friends and friends of friends
I took a shot at a solution. I have 2 classes, one representing a person and the other representing a group.
Person contains his list of friends and some functions to support the queries you want to ask.
/**
* 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() . "
";
}
}
}
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)) . "
";
$sl = $g->suggestedFriendsForEveryone();
echo "Suggested Friends
";
foreach($sl as $row) {
echo "Person {$row['id']} suggested [" . implode(', ', $row['suggested']) . "]
";
}