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 に答える