-3

我想使用 json 解码来获取这些数据。请帮忙..

到目前为止,我想出了这个脚本,但我无法在操作下获取倒数第二个和最后一个“名称”和“链接”。请帮忙..

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

但这不起作用请帮助我想从以下示例中获取所有名称和链接数据,它是来自 facebook graph 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"
            }
          ],
4

1 回答 1

0

请注意,这是一个数组,在您的情况下actions它是唯一的成员。data

您只是迭代错误的部分,代码应该是:

foreach($json['data']['actions'] as $action) {
  $name = $action['name'];
  $link = $action['link'];
}
于 2012-06-29T19:34:33.030 回答