2012-06-29 93 views
-3

我想使用json解碼獲取這些數據。請幫助..如何在php中獲取嵌套的json解碼數據。

到目前爲止,我想出了這個腳本,但我無法取得倒數第二和最後的'名稱'和'鏈接'的行動。請幫助..

foreach($json['data'] as $post) 
{ 
$name1 = $post['actions']['name']; 
$name2 = $post['actions']['name']['name']; 
$name3 = $post['actions']['name']['name']['name']; 

但是,這是行不通的,請幫我想從下面的例子中得到所有名稱和鏈接的數據從Facebook圖形API的一個片段..

{ 
     "data": [ 
     { 

      "actions": [ 
      { 
       "name": "Comment", 
       "link": "http://www.facebook.com/xxxxxidxxxx/posts/xxxxidxxxxxx" 
      }, 
      { 
       "name": "Like", 
       "link": "http://www.facebook.com/xxxxxidxxxxxx/posts/xxxxidxxxxxxx" 
      } 
{ 
       "name": "Comment", 
       "link": "http://www.facebook.com/xxxxxidxxxx/posts/xxxxidxxxxxx" 
      }, 
      { 
       "name": "Like", 
       "link": "http://www.facebook.com/xxxxxidxxxxxx/posts/xxxxidxxxxxxx" 
      } 
{ 
       "name": "Comment", 
       "link": "http://www.facebook.com/xxxxxidxxxx/posts/xxxxidxxxxxx" 
      }, 
      { 
       "name": "Like", 
       "link": "http://www.facebook.com/xxxxxidxxxxxx/posts/xxxxidxxxxxxx" 
      } 
      ], 
+0

如果我只需要最後「名稱」和「鏈接」離開每一個。或第二最後.. –

回答

0

請注意,actions是一個數組,它是您的案例中唯一的成員data

你只是遍歷錯誤的部分,代碼應該是:

foreach($json['data']['actions'] as $action) { 
    $name = $action['name']; 
    $link = $action['link']; 
} 
+0

如果我只需要最後的'名稱'和'鏈接'在行動或我需要第二個最後'名稱'和'鏈接' –

+0

'$ json ['data'] ['actions']'是一個['Array'](http://php.net/Array),做你想做的任何事情,通過'array_shift'-ing或通過索引訪問它的最後一個... –

+0

你能給我舉個例子嗎?通過索引訪問它.. –