如何显示公司名称“;仅“;关于好友工作历史(facebookapi)


How to display company name "only" on friends work history (facebook api)

我一直在获取我的facebook朋友的工作历史。但结果是一个数组,其内容如下:

 {
 "id": "xxx", 
 "friends": {
"data": [
  {
    "name": "Indiarto Priadi", 
    "work": [
      {
        "employer": {
          "id": "111178415566505", 
          "name": "Liputan 6 SCTV"
        }
      }, 
      {
        "employer": {
          "id": "107900732566334", 
          "name": "SCTV"
        }
      }
    ], 
    "id": "502163984"
  }, 
  {
    "name": "Agustin Kertawijaya", 
    "work": [
      {
        "employer": {
          "id": "138215336225242", 
          "name": "Trader Corporation (Canada)"
        }, 
        "location": {
          "id": "110941395597405", 
          "name": "Toronto, Ontario"
        }, 
        "position": {
          "id": "168673399850791", 
          "name": "Desktop operator <Pre press>"
        }, 
        "start_date": "2006-06-01", 
        "end_date": "2008-06-01"
      }, 
      {
        "employer": {
          "id": "114464911939560", 
          "name": "Merrill Corporation Canada"
        }, 
        "location": {
          "id": "110941395597405", 
          "name": "Toronto, Ontario"
        }, 
        "position": {
          "id": "190075304347365", 
          "name": "Financial Print Project Manager"
        }, 
        "start_date": "2006-06-01", 
        "end_date": "2011-10-01"
      }
    ], 
    "id": "511990261"
  }
], 
"paging": {
  "next": "https://graph.facebook.com/1065251285/friends?limit=2&fields=name,work&offset=2&__after_id=511990261"
}

}}

现在我想在我的页面上"只"显示雇主的名字,但我找不到这样做的方法。这是我的代码:

$userFriends = $facebook->api('/'.$userId.'/friends');
for($i=0;$i<=3;$i++){ //retrieved friends (max 4 persons)
        $value=$userFriends["data"][$i];
        echo "<div id='$value[id]'>";
        $profile = $facebook->api("/".$value["id"]);
        $friendsWork = $facebook->api("/".$value["id"]."?fields=work");
        echo "<strong>".$profile["name"]."<br></strong>";
        echo " <img src='https://graph.facebook.com/".$value["id"]."/picture'><br>";
        echo "Username : ".$profile["username"]."<br>";
        echo "Local : ".$profile["locale"]."<br>";
        echo "Birthdate : ".$profile["birthday"]."<br>";
        print_r($friendsWork); // <--- the result is an array
        echo "<hr>";
        echo "</div>";
    }

有人知道如何只显示公司(雇主)名称吗?任何答案都将不胜感激。谢谢

给你,没什么,我只是按照我在评论中说的做了

$json = '{
 "id": "xxx",
 "friends": {
"data": [
  {
    "name": "Indiarto Priadi",
    "work": [
      {
        "employer": {
          "id": "111178415566505",
          "name": "Liputan 6 SCTV"
        }
      },
      {
        "employer": {
          "id": "107900732566334",
          "name": "SCTV"
        }
      }
    ],
    "id": "502163984"
  },
  {
    "name": "Agustin Kertawijaya",
    "work": [
      {
        "employer": {
          "id": "138215336225242",
          "name": "Trader Corporation (Canada)"
        },
        "location": {
          "id": "110941395597405",
          "name": "Toronto, Ontario"
        },
        "position": {
          "id": "168673399850791",
          "name": "Desktop operator <Pre press>"
        },
        "start_date": "2006-06-01",
        "end_date": "2008-06-01"
      },
      {
        "employer": {
          "id": "114464911939560",
          "name": "Merrill Corporation Canada"
        },
        "location": {
          "id": "110941395597405",
          "name": "Toronto, Ontario"
        },
        "position": {
          "id": "190075304347365",
          "name": "Financial Print Project Manager"
        },
        "start_date": "2006-06-01",
        "end_date": "2011-10-01"
      }
    ],
    "id": "511990261"
  }
],
"paging": {
  "next": "https://graph.facebook.com/1065251285/friends?limit=2&fields=name,work&offset=2&__after_id=511990261"
}}}'; // remember the last two closing curlys, you left them out of the code block in the OP.
  $obj = json_decode($json);
  foreach ($obj->friends->data as $friend) {
    echo '<h1>' . $friend->name . '</h1>';
    foreach ($friend->work as $job) {
      echo 'Employee: ' . $job->employer->name . '<br/>';
    }
    echo '<hr>';
  }

输出:

Indiarto Priadi
Employee: Liputan 6 SCTV
Employee: SCTV
--------------------------------------
Agustin Kertawijaya
Employee: Trader Corporation (Canada)
Employee: Merrill Corporation Canada
--------------------------------------

您只需要解析您获得的数组,如下所示-

$userFriends = $facebook->api('/'.$userId.'/friends?fields=work,name');
foreach($userFriends['data'] as $friend)
{
   $friend_name = $friend['name'];
   $emp_name=array();
   // list of all employers of this friend
   if(isset($friend['work']))
   {
       foreach($friend['work'] as $work)
       {
         array_push($emp_name, $work['employer']['name']);
       }
   } 
   else
   {
       $emp_name = "N.A.";
   }
}