1

I am attempting to fetch a JSON file from a separate website, read that file and then decode it into a multidimensional array. So far at that point it seems to work. However, when I try and break it all down into variables to echo, it goes wrong when its 4 levels deep. You can see the actual JSON file from the link in the PHP code and below is the PHP Code I am using. The specific variables that have problems are $prices in 2 areas and $rulings. Any help someone can provide with this would be appreciated.

<?php

$path           = 'https://www.mtgjson.com/json/LEA.json';
$FileContents   = file_get_contents($path);
$cards          = json_decode($FileContents, true);

foreach ($cards['cards'] as $mtgcard){
$Artist                     = $mtgcard['artist'];
$borderColor                = $mtgcard['borderColor'];
$colorIdentity              = $mtgcard['colorIdentity'][0];
$Colors                     = $mtgcard['colors'][0];
$convertedManaCost          = $mtgcard['convertedManaCost'];
$edhrecRank                 = $mtgcard['edhrecRank'];
$flavorText                 = $mtgcard['flavorText'];
$cards_foreignData          = implode($mtgcard['foreignData'],",");
$foreignData                = explode(',', $cards_foreignData);
$frameVersion               = $mtgcard['frameVersion'];
$hasFoil                    = $mtgcard['hasFoil'];
$hasNonFoil                 = $mtgcard['hasNonFoil'];
$isPaper                    = $mtgcard['isPaper'];
$Layout                     = $mtgcard['layout'];
$cards_legalities           = implode($mtgcard['legalities'],",");
$legalities                 = explode(',', $cards_legalities);
$manaCost                   = $mtgcard['manaCost'];
$mcmId                      = $mtgcard['mcmId'];
$mcmMetaId                  = $mtgcard['mcmMetaId'];
$mtgstocksId                = $mtgcard['mtgstocksId'];
$multiverseId               = $mtgcard['multiverseId'];
$name                       = $mtgcard['name'];
$number                     = $mtgcard['number'];
$originalText               = $mtgcard['originalText'];
$originalType               = $mtgcard['originalType'];
$power                      = $mtgcard['power'];
$cards_prices               = implode($mtgcard['prices'],",");
$prices                     = explode(',', $cards_prices);
$cards_prices_2             = implode($cards_prices['paper'],",");
$prices_2                   = explode(',', $cards_prices_2);
$cards_printings            = implode($mtgcard['printings'],",");
$printings                  = explode(',', $cards_printings);
$cards_purchaseUrls         = implode($mtgcard['purchaseUrls'],",");
$purchaseUrls               = explode(',', $cards_purchaseUrls);
$rarity                     = $mtgcard['rarity'];
$cards_rulings              = implode($mtgcard['rulings'],",");
$rulings                    = explode(',', $cards_rulings);
$scryfallId                 = $mtgcard['scryfallId'];
$scryfallIllustrationId     = $mtgcard['scryfallIllustrationId'];
$scryfallOracleId           = $mtgcard['scryfallOracleId'];
$cards_subtypes             = implode($mtgcard['subtypes'],",");
$subtypes                   = explode(',', $cards_subtypes);
$cards_supertypes           = implode($mtgcard['supertypes'],",");
$supertypes                 = explode(',', $cards_supertypes);
$tcgplayerProductId         = $mtgcard['tcgplayerProductId'];
$text                       = $mtgcard['text'];
$toughness                  = $mtgcard['toughness'];
$type                       = $mtgcard['type'];
$types                      = $mtgcard['types'][0];
$uuid                       = $mtgcard['uuid'];

$a       = substr($scryfallId, 0, 1);
$b       = substr($scryfallId, 1, 1);
$convert = "<img src='https://img.scryfall.com/cards/large/front/" .$a. "/" .$b. "/" .$scryfallId. ".jpg' />";

echo $convert;
echo    "Artist Name:  "        .$Artist.               "<br/>";
echo    "Border Color:  "       .$borderColor.          "<br/>";
echo    "colorIdentity:  "      .$colorIdentity.        "<br/>";
echo    "Colors:  "             .$Colors.               "<br/>";
echo    "convertedManaCost:  "  .$convertedManaCost.    "<br/>";
echo    "edhrecRank:  "         .$edhrecRank.           "<br/>";
echo    "flavorText:  "         .$flavorText.           "<br/>";
echo    "foreignData:  "        .$foreignData[0].       "<br/>";
echo    "frameVersion:  "       .$frameVersion.         "<br/>";
echo    "hasFoil:  "            .$hasFoil.              "<br/>";
echo    "hasNonFoil:  "         .$hasNonFoil.           "<br/>";
echo    "isPaper:  "            .$isPaper.              "<br/>";
echo    "Layout:  "             .$Layout.               "<br/>";
echo    "Brawl:  "              .$legalities[0].        "<br/>";
echo    "Commander:  "          .$legalities[1].        "<br/>";
echo    "Duel:  "               .$legalities[2].        "<br/>";
echo    "Future:  "             .$legalities[3].        "<br/>";
echo    "Historic:  "           .$legalities[4].        "<br/>";
echo    "Legacy:  "             .$legalities[5].        "<br/>";
echo    "Modern:  "             .$legalities[6].        "<br/>";
echo    "Oldschool:  "          .$legalities[7].        "<br/>";
echo    "Penny:  "              .$legalities[8].        "<br/>";
echo    "Pioneer:  "            .$legalities[9].        "<br/>";
echo    "Standard:  "           .$legalities[10].       "<br/>";
echo    "Vintage:  "            .$legalities[11].       "<br/>";
echo    "Manacost:  "           .$manaCost.             "<br/>";
echo    "mcmId:  "              .$mcmId.                "<br/>";
echo    "mcmMetaId:  "          .$mcmMetaId.            "<br/>";
echo    "mtgstocksId:  "        .$mtgstocksId.          "<br/>";
echo    "multiverseId:  "       .$multiverseId.         "<br/>";
echo    "name:  "               .$name.                 "<br/>";
echo    "number:  "             .$number.               "<br/>";
echo    "originaltext:  "       .$originalText.         "<br/>";
echo    "originaltype:  "       .$originalType.         "<br/>";
echo    "power:  "              .$power.                "<br/>";
echo    "MTGO prices:  "        .$prices[0].            "<br/>";
echo    "MTGO Foil Prices:  "   .$prices[1].            "<br/>";
echo    "Paper Prices:  "       .prices[2]              "<br/>"; <- 4 levels deep doesnt work
echo    "Paper Foil Prices:  "  .$prices[3].            "<br/>"; <- 4 levels deep doesnt work
echo    "purchaseUrls:  "       .$purchaseUrls[0].      "<br/>";
echo    "purchaseUrls:  "       .$purchaseUrls[1].      "<br/>";
echo    "purchaseUrls:  "       .$purchaseUrls[2].      "<br/>";
echo    "rarity:  "             .$rarity.               "<br/>";
echo    "ruling:  "             .$rulings['date'].$rulings['text'] "<br/>"; <-- 4 levels deep doesnt work
echo    "scryfallId:  "         .$scryfallId.           "<br/>";
echo    "scryfallIllustrationId:  " .$scryfallIllustrationId. "<br/>";
echo    "scryfallOracleId:  "   .$scryfallOracleId.     "<br/>";
echo    "subtypes:  "           .$subtypes[0].          "<br/>";
echo    "supertypes:  "         .$supertypes[0].        "<br/>";    
echo    "tcgplayerProductId:  " .$tcgplayerProductId.   "<br/>";
echo    "text:  "               .$text.                 "<br/>";
echo    "toughness:  "          .$toughness.            "<br/>";
echo    "type:  "               .$type.                 "<br/>";
echo    "types:  "              .$types.                "<br/>";
echo    "uuid:  "               .$uuid.                 "<br/>";

} ?>

0

1 Answer 1

1

You should mention the array name to fetch the content inside array, here is the example code and artist is with small "a" not capital

Edit as per comment request

Get the array of 'legalities' a string with , separated using implode and than explode with , and convert into an array to print those

Note: only the values will be imported using implode not the keys

<?php
// error_reporting(0);
// header('Content-type: application/json');
$path           = 'https://www.mtgjson.com/json/LEA.json';
$FileContents   = file_get_contents($path);
$cards          = json_decode($FileContents, true);

foreach($cards['cards'] as $mtgcard) {

    echo '<br>' .$course_data1 = $mtgcard['artist']; 

    $cards_legalities = implode($mtgcard['legalities'],",");
    $myArray = explode(',', $cards_legalities);
    echo $myArray[0];// 1st array

    //Multidimensional Array for your updated question
    $cards_prices_paper = implode($mtgcard['prices']['paper'],",");
    $prices_paper_array = explode(',', $cards_prices_paper);
    echo $prices_paper_array[0];

    $rulings_date =  implode(', ', array_column($mtgcard['rulings'], 'date'));
    $rulings_date_array =  explode(",", $rulings_date);
    echo $rulings_date_array[0];
}
?>
Sign up to request clarification or add additional context in comments.

7 Comments

Mentioning the array name ['cards'] was what was missing. Thank you. However, the others that have nested arrays will not work. Such as, legalities, prices, printings, purchaseUrls, rulings, subtypes, and supertypes. colorIdentity, colors, and types worked fine by adding [0] like so, $Colors = $mtgcard['colors'][0]; I am also getting undefined index warnings for toughness, power and flavorText?
I've updated my answer please check and comment if you have any doubts
For toughness, power and flavorText -- in some places these are not present at all so you are getting that error, use error_reporting(0);
Thanks for the help. It seems all of this is working now, except for 2 specific areas that go deeper and I didn't notice till now. The variable $prices goes 4 levels deep, so paper prices and paper foil prices will not display and the same with the rulings variable with date and text within its array. I figured the implode method would work too dig deeper but it did not. is there a better way for digging deeper into multidimensional arrays than with deplode/explode? If not, how am I to get to these 4th level arrays and what if there 5 or 6 deep for example? I've updated the code above.
hello, I've updated my answer please check hope it helps else comment again let me try again
|

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.