2015-06-25 12 views
2

我已經將我的對象數據轉換爲數組,現在我試圖從多維數組中提取某些部分,但是我遇到了一些問題。表示讚賞,謝謝。我如何從facebook圖形api數組中提取特定數據?

/* PHP SDK v4.0.0 */ 
/* make the API call */ 
$request = new FacebookRequest(
    $session, 
    'GET', 
    '/89647580016/feed' 
); 
$response = $request->execute(); 
$graphObject = $response->getGraphObject()->AsArray(); 
/* handle the result */ 

    // print data 

echo '<pre>' . print_r($graphObject, 1) . '</pre>'; 

以下是輸出:

Array 
(
    [data] => Array 
     (
      [0] => stdClass Object 
       (
        [id] => 89647580016_10153019927930017 
        [from] => stdClass Object 
         (
          [name] => Central Casting Los Angeles 
          [category] => Local Business 
          [category_list] => Array 
           (
            [0] => stdClass Object 
             (
              [id] => 176831
              [name] => Professional Services 
             ) 

           ) 

          [id] => 89647580016 
         ) 

        [message] => ***NON Union Submissions*** 
Must be registered with Central Casting! 

Jessica is currently booking a TV show working tomorrow Friday June 26th with a possible recall Monday June 29th in LA. These will be Night Calls, so you must be okay working late into the night. 
She is looking for Caucasian looking men, who appear to be in their 30's-50's, who appear to be very upscale, who have business suits. 

If this is you please call submission line 818-260-3952. Thank you! 
        [actions] => Array 
         (
          [0] => stdClass Object 
           (
            [name] => Comment 
            [link] => https://www.facebook.com/89647580016/posts/10153019927930017 
           ) 

          [1] => stdClass Object 
           (
            [name] => Like 
            [link] => https://www.facebook.com/89647580016/posts/10153019927930017 
           ) 

          [2] => stdClass Object 
           (
            [name] => Share 
            [link] => https://www.facebook.com/89647580016/posts/10153019927930017 
           ) 

         ) 

如何可以打印所有[ '消息']?我試過了:

foreach ($graphObject as $key => $value) { 
echo '<br>'.$key['message']; 
} 

但是我得到了一個錯誤。感謝您的幫助。

回答

1

你的數組有一些鍵,其中的元素實際上是StdClass。您不能以$key['message']作爲反駁,但可以作爲:$key->message

此外,不要忘記包含您的錯誤信息。一個錯誤是非常通用的,如果沒有跡象表明什麼是錯誤的,我們不能/不會幫助你。

我沒有注意到你的foreach。由於您只在$graphObject第一級上進行迭代,因此您會得到$key數據,並將其作爲另一個整數數組,其中每個鍵都有一個StdClass。在您的foreach上運行它作爲foreach ($graphObject['data'] as $key => $value),然後使用它作爲echo $value->message

+0

我試過你的解決方案,我沒有得到任何返回。也沒有錯誤信息,只是一個空白頁。 當我試着'echo'
'。$ key; '它打印了單詞'data'。 –

+0

我得到了一個錯誤:警告:爲foreach提供的無效參數() –

+0

是的,我發現後,發現它。它是一個數組。引用數據並用括號括起來,一切都應該沒問題。我再次更新了我的答案,以便說清楚。 –

相關問題