-2

我在这里有这段代码,其中$friends变量是一个输出如下的数组:

{
  "id": "1149862205",
  "name": "name",
  "first_name": "first_name",
  "last_name": "last_name",
  "link": "https://www.facebook.com/username",
  "username": "username",
  "birthday": "07/12/1990",
  "hometown": {
    "id": "108618265828550",
    "name": "name"
  }, 

$fbfriendlikes变量由 id 生成$friends并具有以下结构:

{
  "data": [
    {
      "name": "Penelope-ns-test",
      "category": "Community",
      "id": "187099821418102",
      "created_time": "2012-06-14T15:00:59+0000"
    },

现在,我需要在 我的代码中打印所有$friends类似于特定 id 的 id,如下所示:$fbfriendlikes

$friends = $facebook->api('/me/friends');
if (!empty($friends['data'])) {
    $size = variable_get('facebook_graph_pic_size_nodes','square');
    $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
    foreach ($friends['data'] as $data) {
        $fbid = $data['id'];
        $fbfriendlikes[$fbid]=$facebook->api('/'.$fbid.'/likes'); 
        $fbname = $data['name'];
        $path = $protocol . '://graph.facebook.com/' . $fbid . '/picture?type=' . $size;
        $image = theme('image', array('path' => $path, 'alt' => $fbname));

        if ($fbfriendlikes["id"] != 184759054887922) {
            $return .= '<div class="fimage">'.$image.'</div>';
            $link = '<a href="'.$protocol . '://www.facebook.com/profile.php?id='.$fbid.'" target="_blank">'.$fbname.'</a>';    
            $return .= '<div class="flink">'.$link.'</div>';
            break;
        }
    }
}

我知道我必须做一个 foreach 循环,$fbfriendslikes但我似乎无法让它工作。你能给我一个建议吗?

4

2 回答 2

0

试试这个:

$return = '';
$friends = $facebook->api('/me/friends');
if (!empty($friends['data'])) {
    $size = variable_get('facebook_graph_pic_size_nodes','square');
    $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
    foreach ($friends['data'] as $data) {
        $fbid = $data['id'];
        $fbfriendlikes[$fbid]=$facebook->api('/'.$fbid.'/likes'); 
        $fbname = $data['name'];
        $path = $protocol . '://graph.facebook.com/' . $fbid . '/picture?type=' . $size;
        $image = theme('image', array('path' => $path, 'alt' => $fbname));

        $likes = false;
        foreach($fbfriendlikes['data'] as $like) {
            if($like['id'] == '184759054887922') { // maybe $like->id will have to be used instead...
                $likes = true;
                break;
            }
        }

        if (!$likes) {
            $return .= '<div class="fimage">'.$image.'</div>';
            $link = '<a href="'.$protocol . '://www.facebook.com/profile.php?id='.$fbid.'" target="_blank">'.$fbname.'</a>';    
            $return .= '<div class="flink">'.$link.'</div>';
        }
    }
    echo $return;
}
于 2012-06-26T08:45:09.793 回答
0

您需要检查循环内部以获取不喜欢的循环。

$return = '';
$friends = $facebook->api('/me/friends');
if (!empty($friends['data'])) {
    $size = variable_get('facebook_graph_pic_size_nodes','square');
    $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
    foreach ($friends['data'] as $data) {
        $fbid = $data['id'];
        $fbfriendlikes[$fbid]=$facebook->api('/'.$fbid.'/likes'); 
        $fbname = $data['name'];
        $path = $protocol . '://graph.facebook.com/' . $fbid . '/picture?type=' . $size;
        $image = theme('image', array('path' => $path, 'alt' => $fbname));

        foreach($fbfriendlikes['data'] as $like) {
            $likes = false; // set var to check for like
            if($like['id'] == '184759054887922') { // check if they liked it
                $likes = true; // set var to true because they liked it
            }
            if (!$likes) { // test var to see if false (they did not like)
                // print this if they did NOT like it, otherwise do nothing
                $return .= '<div class="fimage">'.$image.'</div>';
                $link = '<a href="'.$protocol . '://www.facebook.com/profile.php?id='.$fbid.'" target="_blank">'.$fbname.'</a>';    
                $return .= '<div class="flink">'.$link.'</div>';
            }
        }// go on to next in loop

    }
    echo $return;
}
于 2012-06-26T12:37:50.487 回答