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