PHP-从JSON文件中提取数组(对象?)

PHP-从JSON文件中提取数组(对象?),php,arrays,json,Php,Arrays,Json,我有一个JSON文件,我想通过PHP访问内容。问题在于访问JSON文件中的数组。这个网站上建议的其他方法似乎不起作用。下面是JSON结构的一个示例。这里的PHP代码是打开和关闭PHP标记之间唯一的PHP代码 这段PHP代码可以正常工作。我正在访问一个不是数组的东西 $jsondata = file_get_contents('BFZ.json'); $data = json_decode($jsondata, true); $id = $data['name']; echo $id; 这不起作

我有一个JSON文件,我想通过PHP访问内容。问题在于访问JSON文件中的数组。这个网站上建议的其他方法似乎不起作用。下面是JSON结构的一个示例。这里的PHP代码是打开和关闭PHP标记之间唯一的PHP代码

这段PHP代码可以正常工作。我正在访问一个不是数组的东西

$jsondata = file_get_contents('BFZ.json');
$data = json_decode($jsondata, true);
$id = $data['name'];
echo $id;
这不起作用。我正在尝试访问JSON文件中“cards”数组(object?)的“name”部分

$jsondata = file_get_contents('BFZ.json');
$data = json_decode($jsondata, true);
$id = $data['cards']['name'];
echo $id;
这也不起作用:

$id = $data['cards']['name'][0];
JSON文件的结构和示例信息:

              "name" : "Nemesis",
              "code" : "NMS",
      "gathererCode" : "NE",
           "oldCode" : "NEM",
"magicCardsInfoCode" : "ne",
       "releaseDate" : "2000-02-14",
            "border" : "black",
              "type" : "expansion",
             "block" : "Masques",
        "onlineOnly" : false,
           "booster" : [ "rare", ... ],
             "cards" : [ {}, {}, {}, ... ]
           "name" : "Sen Triplets",
       "manaCost" : "{2}{W}{U}{B}",
            "cmc" : 5,
         "colors" : ["White", "Blue", "Black"],
           "type" : "Legendary Artifact Creature — Human Wizard",
     "supertypes" : ["Legendary"],
          "types" : ["Artifact", "Creature"],
       "subtypes" : ["Human", "Wizard"],
         "rarity" : "Mythic Rare",
           "text" : "At the beginning of your upkeep, choose target opponent.
                     This turn, that player can't cast spells or activate
                     abilities and plays with his or her hand revealed.
                     You may play cards from that player's hand this turn.",
         "flavor" : "They are the masters of your mind.",
         "artist" : "Greg Staples",
         "number" : "109",
          "power" : "3",
      "toughness" : "3",
         "layout" : "normal",
   "multiverseid" : 180607,
      "imageName" : "sen triplets",
             "id" : "3129aee7f26a4282ce131db7d417b1bc3338c4d4"
JSON文件的“cards”数组(object?)的结构以及示例信息:

              "name" : "Nemesis",
              "code" : "NMS",
      "gathererCode" : "NE",
           "oldCode" : "NEM",
"magicCardsInfoCode" : "ne",
       "releaseDate" : "2000-02-14",
            "border" : "black",
              "type" : "expansion",
             "block" : "Masques",
        "onlineOnly" : false,
           "booster" : [ "rare", ... ],
             "cards" : [ {}, {}, {}, ... ]
           "name" : "Sen Triplets",
       "manaCost" : "{2}{W}{U}{B}",
            "cmc" : 5,
         "colors" : ["White", "Blue", "Black"],
           "type" : "Legendary Artifact Creature — Human Wizard",
     "supertypes" : ["Legendary"],
          "types" : ["Artifact", "Creature"],
       "subtypes" : ["Human", "Wizard"],
         "rarity" : "Mythic Rare",
           "text" : "At the beginning of your upkeep, choose target opponent.
                     This turn, that player can't cast spells or activate
                     abilities and plays with his or her hand revealed.
                     You may play cards from that player's hand this turn.",
         "flavor" : "They are the masters of your mind.",
         "artist" : "Greg Staples",
         "number" : "109",
          "power" : "3",
      "toughness" : "3",
         "layout" : "normal",
   "multiverseid" : 180607,
      "imageName" : "sen triplets",
             "id" : "3129aee7f26a4282ce131db7d417b1bc3338c4d4"

我从这里获得了JSON文件:。该文件引用了纸牌游戏魔术:聚会。我之所以使用PHP,是因为我的目的是最终将数据加载到MySQL数据库中。

您瞄准的$data['name']就是扩展中的一个

您需要访问数组['cards']以访问卡列表,然后通过循环可以获得所有卡的名称

您可能希望这样做:

$jsondata = file_get_contents('http://mtgjson.com/json/BFZ.json');
$data = json_decode($jsondata, true);

$cards = $data['cards'];

$cardsName = array();
foreach ($cards as $card) {
    $cardsName[] = $card['name'];
}

var_dump($cardsName);

这将创建一个数组,该数组将包含所有卡的名称,看起来像是
键包含一个json对象数组
json_decode()
将对其进行解析


鉴于此,
$data['cards'][0]['name']
应该为您提供第一张卡的名称。类似地,
$data['cards'][1]['name']
应该给出第二张卡的名称。

您需要取消序列化数据

$jsondata = file_get_contents('http://mtgjson.com/json/BFZ.json');
$data = unserialize($jsondata);  
// Show the unserialized data;  
var_dump ($data);

这就是我想要的答案:直接访问项目。效果很好。