php数组,循环得到确切的值 [英] php array, loop to get the exact value

查看:79
本文介绍了php数组,循环得到确切的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有这个代码,其中 $ 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 并具有以下结构:

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

现在,我需要在 $ fbfriendlikes $中打印所有 $ friends b $ b我的c ode如下所示:

  $ 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 ='。 $大小;
$ 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;
}
}
}

我知道我必须在 $ fbfriendslikes 中执行foreach循环,但我似乎无法使其工作
你能给我一个建议吗?

解决方案

你需要检查里面的循环才能得到那些

  $ 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 ='。 $大小;
$ 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'){//检查他们是否喜欢
$ likes = true; //将var设置为true,因为它们喜欢它
}
if(!$ likes){//测试var查看是否为false(他们不喜欢)
//打印出来,如果他们不喜欢它,否则不做任何
$ 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;
}


I have this code right here, where the $friends variable is an array with an output like this:

{
  "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"
  }, 

while $fbfriendlikes variable is generated by the id of $friends and has this structure :

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

Now, I need to print all $friends ids which like a specific id in $fbfriendlikes My code goes like this:

$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;
        }
    }
}

I know I have to do a foreach loop in $fbfriendslikes but I can't seem to make it work. Can you please give me an advice?

解决方案

You need to check inside you loop to get the ones that don't like.

$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;
}

这篇关于php数组,循环得到确切的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆