Just realised it should probably be this:
<?php
function searchPerson($person, $friends) {
$direct = false;
$found = false;
if (in_array($person, array_keys($friends))) {
list($total, $common_friends) = commonFriend($person, $friends);
$direct = true;
$found = true;
} else {
foreach ($friends as $friend => $his_friends) {
if (in_array($person, $his_friends)) {
list($total, $common_friends) = commonFriend($friend, $friends);
$direct = false;
$found = true;
$friend_person = $friend;
}
}
}
if (!$found) {
$output = "{$person} is not on the list";
} else {
$output = "{$person} ";
if ($direct) {
$output .= " has {$total} friends";
} else {
$output .= " is {$friend_person}'s friend who has {$total} friends";
}
if (isset($common_friends[0])) $output .= " and common friends with {$common_friends}";
}
return $output;
}
function commonFriend($person, $friends) {
$my_friends = $friends[$person];
unset($friends[$person]);
$total_friends = count($my_friends);
$common_with = array();
foreach ($friends as $friend => $his_friends) {
foreach ($my_friends as $my_friend) {
if (in_array($my_friend, $his_friends)) {
$common_with[] = $friend;
}
}
}
$common_with = array_unique($common_with);
$common_friends = "";
if (count($common_with) > 0) {
$common_friends = join(", ", $common_with);
}
return array($total_friends, $common_friends);
}
$friends = array(
"Rana" => array("Pothik", "Zaman", "Tanmoy", "I****a"),
"Nandini" => array("Bonna", "Shakib", "Kamal", "Minhaj", "I****a"),
"Roni" => array("Akbar", "Anwar", "Khakan", "Pavel"),
"Liton" => array("Mahadi", "Pavel"),
"Mamun" => array("Meheli", "Tarek", "Zaman")
);
print searchPerson($_GET['person'], $friends);
?>
Don't forget to refresh the page instead of just changing the URL in the browser, so it isn't cached. Or you could add...
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
before the print() call