【问题标题】:PHP - Extracting an array (object?) from a JSON filePHP - 从 JSON 文件中提取数组(对象?)
【发布时间】:2015-11-01 03:22:15
【问题描述】:

我有一个 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 文件中“卡片”数组(对象?)的“名称”部分。

$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" : [ {}, {}, {}, ... ]

带有示例信息的 JSON 文件的“卡片”数组(对象?)的结构:

           "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 文件:http://mtgjson.com/。该文件引用了纸牌游戏 Magic: the Gathering。我使用 PHP 是因为我的目的是最终将数据加载到 MySQL 数据库中。

【问题讨论】:

    标签: php arrays json


    【解决方案1】:

    看起来cards 键包含一个 json 对象数组。 json_decode() 会这样解析它。

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

    【讨论】:

    • 这是我一直在寻找的答案:直接访问这些项目。效果很好。
    【解决方案2】:

    您所定位的 $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);
    

    这将创建一个包含所有卡片名称的数组

    【讨论】:

      【解决方案3】:

      你需要反序列化你的数据

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-22
        • 2019-09-20
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 1970-01-01
        • 2021-03-06
        • 2010-11-10
        相关资源
        最近更新 更多