1

I've got a JSON file and I want to access the contents via PHP. The problem is accessing an array inside the JSON file. Other methods suggested on this site don't seem to work. An example of the JSON structure is at the bottom. The PHP code here is the only PHP code between my opening and closing PHP tags.

This PHP code works. I'm accessing something that isn't an array.

$jsondata = file_get_contents('BFZ.json');
$data = json_decode($jsondata, true);
$id = $data['name'];
echo $id;

This doesn't work. I'm trying to access the "name" portion of the "cards" array (object?) in the JSON file.

$jsondata = file_get_contents('BFZ.json');
$data = json_decode($jsondata, true);
$id = $data['cards']['name'];
echo $id;

This also doesn't work:

$id = $data['cards']['name'][0];

The structure of the JSON file with example info:

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

The structure of the "cards" array (object?) of the JSON file with example info:

           "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"

I got the JSON file from here: http://mtgjson.com/ . The file references the card game Magic: the Gathering. I'm using PHP because my intention is to eventually load the data into a MySQL database.

3 Answers 3

2

It looks like cards key holds an array of json objects. json_decode() will parse it as such.

Given that, $data['cards'][0]['name'] should give you the name of first card. Analogically, $data['cards'][1]['name'] should give you name of the second card.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the answer I was looking for: direct access to the items. Works great.
0

The $data['name'] you were targeting was the one from the extension.

You need to access the array ['cards'] to reach the card list then with a loop you can get all the cards name.

you might want to do that :

$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);

This will create an array will all the cards name

Comments

0

You need to unserialize your data

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.