【问题标题】:PHP Json_decode Multidimensional ArrayPHP Json_decode 多维数组
【发布时间】:2017-04-28 23:25:05
【问题描述】:

我正在尝试在 PHP 中解码一部分 json 代码。 json 看起来像:

"title":"A Title Here",
"images":[

{
    "coverType":"fanart",
    "url":"some_random_file_here.jpg"
},
{
    "coverType":"banner",
    "url":"another_random_file_here.jpg"
},
{
    "coverType":"poster",
    "url":"yet_another_random_file_here.jpg"
}

],

我想获取 "coverType":"banner"

下的 url

我可以使用以下代码轻松解析标题:

$itemNr = 0;
            foreach($json as $item) {
                $mytitle = $item['title'];
                echo $mytitle;
$itemNr++;

如果使用相同的概念,我的代码会是什么样子。请注意,我已经简化了 JSON 的代码。我上面的代码中未显示的某些项目的实际 php 类似于:

$somevariable = $item['series']['tvdbId'];

非常感谢任何建议。

谢谢,

H.

【问题讨论】:

  • foreach ($item['images'] as $v)
  • 谢谢。我想要做的是只返回 coverType: 横幅下的“url”值。我想忽略其他人。我猜它会是这样的:$myurl = $item['images']['coveType:banner']'[url'];

标签: php json multidimensional-array


【解决方案1】:

如果您可以访问 images 键,则:

<?php
$json = <<<JSON
{
    "title":"A Title Here",
    "images":[
        {
            "coverType":"fanart",
            "url":"some_random_file_here.jpg"
        },
        {
            "coverType":"banner",
            "url":"another_random_file_here.jpg"
        },
        {
            "coverType":"poster",
            "url":"yet_another_random_file_here.jpg"
        }
    ]
}
JSON;

$json = json_decode($json);
print_r($json);

foreach ($json->images as $img)
{
    if ( $img->coverType == "banner" )
    {
        echo 'Image Cover Type: ' .$img->coverType .'<br/>';
        echo 'URL: ' .$img->url .'<br/>';
    }
}
?>

给予:

图片封面类型:横幅

网址:another_random_file_here.jpg

更新:

您链接到的 JSON 文件似乎无效,每个系列后面都缺少大括号。这是更正后的 JSON 和代码:

<?php
$json = <<<JSON
[
{
    "series": {
      "title": "Brooklyn Nine-Nine",
      "images": [
        {
          "coverType": "fanart",
          "url": "http://thetvdb.com/banners/fanart/original/269586-15.jpg"
        },
        {
          "coverType": "banner",
          "url": "http://thetvdb.com/banners/graphical/269586-g3.jpg"
        },
        {
          "coverType": "poster",
          "url": "http://thetvdb.com/banners/posters/269586-13.jpg"
        }
      ],
      "year": 2013
    }
},
{
    "series": {
      "title": "The Middle",
      "images": [
        {
          "coverType": "fanart",
          "url": "http://thetvdb.com/banners/fanart/original/95021-16.jpg"
        },
        {
          "coverType": "banner",
          "url": "http://thetvdb.com/banners/graphical/95021-g14.jpg"
        },
        {
          "coverType": "poster",
          "url": "http://thetvdb.com/banners/posters/95021-8.jpg"
        }
      ],
      "year": 2009
    }
},
{
    "series": {
      "title": "New Girl",
      "images": [
        {
          "coverType": "fanart",
          "url": "http://thetvdb.com/banners/fanart/original/248682-43.jpg"
        },
        {
          "coverType": "banner",
          "url": "http://thetvdb.com/banners/graphical/248682-g20.jpg"
        },
        {
          "coverType": "poster",
          "url": "http://thetvdb.com/banners/posters/248682-14.jpg"
        }
      ],
      "year": 2011
    }
}
]
JSON;

$json = json_decode($json);
// echo '<pre>' .print_r($json, 1) .'</pre>';

foreach ($json as $item)
{
    echo 'Title: ' .$item->series->title .'<br/>';
    foreach ($item->series->images as $img)
    {
        if ( $img->coverType == "banner" )
        {
            echo 'Image Cover Type: ' .$img->coverType .'<br/>';
            echo 'URL: ' .$img->url .'<br/>';
        }
    }
}
?>

给予:

标题:布鲁克林九九 图片封面类型:横幅 网址:http://thetvdb.com/banners/graphical/269586-g3.jpg

标题:中间 图片封面类型:横幅 网址:http://thetvdb.com/banners/graphical/95021-g14.jpg

标题:新女孩 图片封面类型:横幅 网址:http://thetvdb.com/banners/graphical/248682-g20.jpg

【讨论】:

  • 谢谢斯图尔特。正如我在上面的评论中所描述的,我只想获取位于 coverType:banner 下的 url 的值。基本上我想要一个变量,它将返回我的示例中的值“another_random_file_here.jpg”。我不知道这个值是多少,因为它们会有很大的不同。我希望这是有道理的。
  • 对不起斯图尔特。不是一个大专家......我的 json 字符串将包含可能的条目,我创建了一个更扩展的 JSON 输出示例。 See Pastebin。不知道如何创建我的代码,以便我可以获得每个条目的“标题”,每个条目的横幅“url”。我只是不明白您的答案将如何与 Title 以及我需要为每个数组条目抓取的其他数据相关联。一百万感谢您的帮助。
  • 我会更新我的答案。您的 JSON 也无效 - 每个系列都缺少大括号...
  • 谢谢斯图尔特....这就是答案。我非常感谢您的帮助。我已经尝试了几个小时。无效的 JSON 可能是因为我为了清楚起见简化了代码,而且我确信我删除了一些东西。非常非常感谢!
【解决方案2】:

假设这是 jsondecoded 变量, $json[images][1]->coverType;

【讨论】:

  • 你能详细说明这个答案吗?如果这是另一个问题或答案,我会错过。
  • '$json=$json_decode($variablenam); ' ' $json[images][1]->coverType; ' 在 json 中看到,您可以通过对象访问每个对象,例如您可以执行 images->covertype 但您可以看到covertype数据在数组中,因此我们使用 image[array pos postion of covertype]->covertype;
猜你喜欢
  • 2015-03-11
  • 2013-02-02
  • 1970-01-01
  • 2020-11-05
  • 2013-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多