【问题标题】:Illegal String Offset in Multidimensional Array多维数组中的非法字符串偏移
【发布时间】:2019-10-27 02:53:02
【问题描述】:

我正在尝试从单独的网站获取 JSON 文件,读取该文件,然后将其解码为多维数组。到目前为止,它似乎有效。但是,当我尝试将其全部分解为变量以进行回显时,它在 4 级深度时会出错。您可以从 PHP 代码中的链接看到实际的 JSON 文件,下面是我正在使用的 PHP 代码。有问题的具体变量是 2 个区域的 $prices 和 $rules。有人可以提供任何帮助,我们将不胜感激。

<?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/>";

} ?>

【问题讨论】:

    标签: php arrays json multidimensional-array


    【解决方案1】:

    你应该提到数组名称来获取数组中的内容,这里是示例代码,艺术家是小写的“a”而不是大写

    根据评论请求进行编辑

    得到一个字符串的“合法性”数组,用 implode 分隔,然后用 explode 分解,然后转换成一个数组来打印这些

    注意:只有值会使用 implode 导入,而不是键

    <?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];
    }
    ?>
    

    【讨论】:

    • 没有提到数组名称 ['cards']。谢谢你。但是,其他嵌套数组将不起作用。例如,合法性、价格、印刷、purchaseUrls、规则、子类型和超类型。 colorIdentity,颜色和类型通过像这样添加 [0] 可以正常工作,$Colors = $mtgcard['colors'][0];我还收到关于韧性、力量和风味文本的未定义索引警告?
    • 我已经更新了我的答案,如果您有任何疑问,请检查并评论
    • 对于韧性、力量和风味文本——在某些地方这些根本不存在,因此您会遇到该错误,请使用 error_reporting(0);
    • 感谢您的帮助。似乎所有这些现在都在起作用,除了两个更深入的特定区域,我直到现在才注意到。变量 $prices 有 4 个级别,因此不会显示纸张价格和纸箔价格,并且与数组中包含日期和文本的规则变量相同。我认为内爆方法会更深入地挖掘,但事实并非如此。有没有比使用 deplode/explode 更深入地挖掘多维数组的方法?如果没有,我该如何进入这些第 4 级阵列,例如,如果有 5 或 6 个深度怎么办?我已经更新了上面的代码。
    • 你好,我已经更新了我的答案,请检查希望它有助于其他评论让我再试一次
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 2013-09-30
    相关资源
    最近更新 更多