0

我一直在获取我的 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>";
    }

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

4

2 回答 2

1

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

$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
--------------------------------------
于 2013-07-29T10:58:23.153 回答
1

你只需要解析你获得的数组,就像这样 -

$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.";
   }
}
于 2013-07-29T08:50:54.570 回答