2016-11-12 10 views
0

Facebook Graph Apiから[メッセージ]や[作成日時]のような投稿データにアクセスするにはどうすればよいですか?アレイでアレイにアクセスする方法は?

は、ここで私はFacebookのグラフAPIから取得していますJSONのますprint_r、ある

Array 
( 
[posts] => Array 
    ( 
     [data] => Array 
     ( 
      [0] => Array 
      ( 
      [message] => random message. 
      [created_time] => 2016-11-12T07:26:23+0000 
      [id] => id786456786 
      )             
      [1] => Array 
      ( 
      [message] => random message. 
      [created_time] => 2016-11-11T04:30:02+0000 
      [id] => id123456768 
      ) 
     ) 
     [paging] => Array 
     ( 
     [previous] => https://graph.facebook.com/v2.8/pageid/posts?limit=2&since=1478935583&access_token=MY ACCESS TOKEN ....   
     ) 
    ) 
    [id] => id 
) 

私のPHPコード:すべてのヘルプは感謝

$data = file_get_contents("https://graph.facebook.com/v2.8facebookpageid?fields=posts.limit(2)&access_token=MY_ACCESS_TOKEN"); 

$decoded = json_decode($data,true); 

print_r($decoded); 

+1

'$ '[' key '] [' subkey '] [' subsubkey ']'等 – JimL

+0

$デコード[' posts '] [' data '] [0] [' message '] ' – SergeyLebedev

+0

ありがとうございます手伝い! – Gintas

答えて

0

あなたは単に配列のキーをたどることができます。

さらに良い
$decoded = json_decode($data, true); 

// Retrieve the first message. 
$message = $decoded['posts']['data'][0]['message'] 

// Retrieve the second message. 
$message = $decoded['posts']['data'][1]['message'] 

、あなたのようなので、$decoded['posts']['data']をループすることができます

$decoded = json_decode($data, true); 

foreach ($decoded['posts']['data'] as $item) { 
    echo "Message: {$item['message']}\n"; 
    echo "Created Time: {$item['created_time']}\n"; 
} 

PHP Array hereについては、こちらをご覧ください。

このヘルプが必要です。

+0

助けてくれてありがとう! – Gintas

関連する問題