组合Facebook Graph返回的多个JSON数组

时间:2011-06-24 20:25:45

标签: php json facebook facebook-graph-api

我已经尝试过搜索如何合并数组,也许我有一个Facebook特定的问题。我正在尝试合并包含特定人员将要访问的事件数据的数组。这是我到目前为止的PHP:

    //Define FQL Query
$fql = "SELECT uid,education,name FROM user WHERE uid IN (SELECT uid1 FROM friend WHERE uid2=me())";
$param = array(
                'method'    => 'fql.query',
                'query'     => $fql,
                'callback'  => '');
//Execute Query
$fqlres = $facebook->api($param);
//Start Blank Array
$friend_events_array = '';
//Loop through results and display events of students of a particular college
foreach($fqlres as $friend){
    if($friend['education'] == ''){
        //If friend does not have an education_history array display Nothing
    }
    else {
        foreach($friend["education"] as $friendEd) {
            //If friend is a college student
            if($friendEd["type"] == "College") {
                $collegeinfo = $friendEd;
                $schoolinfo_friend = $collegeinfo['school'];
                    //If the name of the friend's college is the same as the user's college.
                    if ($schoolinfo_friend['name'] == $schoolinfo_user['name']){
                        echo $friend['name'].": ".$schoolinfo_friend['name'];
                        echo "<br>";
                        //Query Graph for Events
                        $friend_events = $facebook->api($friend['uid']."/events");
                        //Display Events
                        print_r($friend_events);
                        //Merge Arrays
                        $friend_events_array_new = array_merge($friend_events, $friend_events_array);
                        //Rename Array for Looping
                        $friend_events_array = $friend_events_array_new;
                    }

            }

        } 

    }
}

首先,我获得了所有用户朋友的列表(我拥有所有必要的权限,例如education_history,friends_events和user_education_history。我首先浏览列表并获取在其用户对象中拥有教育数组的所有用户。然后我进入每个教育阵列,看看他们是否是一名大学生,然后我检查他们的大学是否与用户的大学相匹配。如果全部检查出来,那么我对图表进行API调用以得到他们的数组事件。每个事件数组都成功返回,但我想知道是否可以尝试将Loop中的每个新事件数组合并到一个主事件数组中,该事件数组将与每个处理过的朋友一起增长。

思想?

此外,每个单独的事件数组如下所示:

Array ( [data] => Array ( [0] => Array ( [name] => WU Alumni Meet [start_time] => 2011-10-21T17:00:00 [end_time] => 2011-10-23T13:00:00 [location] => Athletic Complex [id] => 197913876920275 [rsvp_status] => attending ) ) [paging] => Array ( [previous] => https://graph.facebook.com/3100772/events?limit=25&since=1319216400 [next] => https://graph.facebook.com/3100772/events?limit=25&until=1319216400 ) )

这有关系吗?

1 个答案:

答案 0 :(得分:0)

如果我正确理解了这一点,那么您要做的就是创建所有用户朋友的独特事件的组合列表。

在您的示例中,$ friend_events_array看起来像是来自所有用户朋友的所有事件的数组。如果是这种情况,那么我相信你已经在创建一个正在扩展的数组。您应该查看PHP中的array_unique函数,以使您的数组值唯一。

  print_r($friend_events);
  //Merge Arrays
  $friend_events_array_new = array_merge($friend_events, $friend_events_array);
  //Rename Array for Looping
  $friend_events_array = $friend_events_array_new;
  $friend_events_array = array_unique($friend_events_array);