duandu2980 2018-02-05 00:23
浏览 32

推荐的朋友php

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

  • 写回答

1条回答 默认 最新

  • doujia2463 2018-02-05 02:42
    关注

    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']) . "]
    ";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题