需要帮助PHP作业 - 朋友匹配算法

时间:2011-01-16 17:47:19

标签: php

我是php的新手并且已经开始学习它了。我在php和html中有两个家庭作业。

作业1:

  1. 我必须存储一些人的姓名和所有朋友的名字。我必须只列出有共同朋友的人。我的问题是,如果一个人没有与其他人共同的朋友,我会收到一条消息“Rana和Roni有0个朋友。我会阻止这个:
  2. 作业2:

    我有一个html表单来搜索上一个php文件中的人

    1. 当我搜索Rana时,PHP表单将打开并打印:

        

      Rana有4个朋友,他和Nandini和Mamun有一个共同的朋友。

    2. 当我搜索Tanmoy时,页面将打开并打印:

        

      Tanmoy是Rana的朋友,他与Nandini和Mamun有4个朋友和朋友。

    3. 为此,我必须使用“post / get / request”函数

    4. 到目前为止,这是我的代码:

      <?php
         # Function: finfCommon
      
      function findCommon($current, $arr) {
          $cUser = $arr[$current];
          unset($arr[$current]);
          foreach ($arr As $user => $friends) {
              $common = array();
              $total = array();
              foreach ($friends As $friend) {
                  if (in_array($friend, $cUser)) {
                      $common[] = $friend;
                  }
              }
              $total = count($common);
              $add = ($total != 1) ? 's' : '';
              $final[] = "<i>{$current} has {$total} friend{$add} in common with {$user}.</i>";
          }
          return implode('<br />', $final);
      }
      
      # Array of users and friends
      
      $Friends = array(
          "Rana" => array("Pothik", "Zaman", "Tanmoy", "Ishita"),
          "Nandini" => array("Bonna", "Shakib", "Kamal", "Minhaj", "Ishita"),
          "Roni" => array("Akbar", "Anwar", "Khakan", "Pavel"),
          "Liton" => array("Mahadi", "Pavel"),
          "Mamun" => array("Meheli", "Tarek", "Zaman")
      );
      
      # Creating the output value
      
      $output = "<ul>";
      foreach ($Friends As $user => $friends) {
          $total = count($friends);
          $common = findCommon($user, $Friends);
          $output .= "<li><u>{$user} has {$total} friends.</u><br /><strong>Friends:</strong>";
          if (is_array($friends) && !empty($friends[0])) {
              $output .= "<ul>";
              foreach ($friends As $friend) {
                  $output .= "<li>{$friend}</li>";
              }
              $output .= "</ul>";
          }
          $output .= "{$common}<br /><br /></li>";
      }
      $output .= "</ul>";
      
      # Printing the output value
      print $output;
      ?>
      

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

更改findCommon功能
function findCommon($current, $arr) {
    $cUser = $arr[$current];
    unset($arr[$current]);
    foreach ($arr As $user => $friends) {
        $common = array();
        $total = array();
        foreach ($friends As $friend) {
            if (in_array($friend, $cUser)) {
                $common[] = $friend;
            }
        }
        $total = count($common);
        $add = ($total != 1) ? 's' : '';
        $final[] = "<i>{$current} has {$total} friend{$add} in common with {$user}.</i>";
    }
    return implode('<br />', $final);
}

function findCommon($current, $arr) {
    $cUser = $arr[$current];
    unset($arr[$current]);
    foreach ($arr As $user => $friends) {
        $common = array();
        $total = array();
        foreach ($friends As $friend) {
            if (in_array($friend, $cUser)) {
                $common[] = $friend;
            }
        }
        $total = count($common);
        $add = ($total != 1) ? 's' : '';
        if ( $total > 0 ) $final[] = "<i>{$current} has {$total} friend{$add} in common with {$user}.</i>";
    }
    return implode('<br />', $final);
}

我在if ( $total > 0 )

之前添加了$final[] = ..

为第二个。

<?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;
                break;
            }
        }
    }
    if ( !$found ) return false;
    $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", "Ishita"),
    "Nandini" => array("Bonna", "Shakib", "Kamal", "Minhaj", "Ishita"),
    "Roni" => array("Akbar", "Anwar", "Khakan", "Pavel"),
    "Liton" => array("Mahadi", "Pavel"),
    "Mamun" => array("Meheli", "Tarek", "Zaman")
);


$person = $_GET['person'];

$output = searchPerson($person, $friends);

if ( $output === false ) print $person . " is not on the list";
else print $output;

?>

与第二项任务有关的是你需要一份表格。

我们可以说上面的代码名为searchPerson.php

然后添加一个像

这样的html页面
<html>
    <head>
        <title>Search for friend</title>
    </head>
    <body>
        <form action="searchPerson.php" method="get">
            Person: <input type="text" name="person" /><input type="submit" value="search" />
        </form>
    </body>
</html>

或直接运行

searchPerson.php?person=Rana

为什么你得到这样的消息是因为$person = $_GET['person'];从网址获取了人名,而你必须像searchPerson.php那样运行它,所以他们没有价值可供$ person检查。

- 编辑

查找代码以与不在列表中的人一起工作