遍历缺少索引的 Json 文件

克里斯来自卡利

我有一个 Json 文件$championsList,它在其索引中跳过一堆数字。例如,$championsList["data"][46]不存在,在底部它从 268 跳到 412。还有一堆更分散的。

我尝试编写一个 for 循环来遍历["data"]索引作为测试。

for($i=1; $i <= sizeof($championsList["data"]; $i++) {
    echo $championsList["data"][$i]["key"];
    }

这样做的问题是,["data"]索引的大小是 136,所以当它到达循环末尾时,它会执行echo $championsList["data"][136]["key"];并停止,跳过数字大于 136 的所有内容,因为它还会遍历不存在的索引,例如echo $championsList["data"][$46]["key"];

我知道我能做到

for($i=1; $i <= 498; $i++) {
    echo ...
}

但这似乎效率很低,当新角色添加到游戏中时,我必须不断调整它

我怎样才能让它迭代并跳过所有丢失的数字?

Json 文件:

{
  "type": "champion",
  "version": "7.10.1",
  "data": {
    "1": {
      "id": 1,
      "key": "Annie",
      "name": "Annie",
      "title": "the Dark Child"
    },
    "2": {
      "id": 2,
      "key": "Olaf",
      "name": "Olaf",
      "title": "the Berserker"
    },
    "3": {
      "id": 3,
      "key": "Galio",
      "name": "Galio",
      "title": "the Colossus"
    },
    "4": {
      "id": 4,
      "key": "TwistedFate",
      "name": "Twisted Fate",
      "title": "the Card Master"
    },
    "5": {
      "id": 5,
      "key": "XinZhao",
      "name": "Xin Zhao",
      "title": "the Seneschal of Demacia"
    },
    "6": {
      "id": 6,
      "key": "Urgot",
      "name": "Urgot",
      "title": "the Headsman's Pride"
    },
    "7": {
      "id": 7,
      "key": "Leblanc",
      "name": "LeBlanc",
      "title": "the Deceiver"
    },
    "8": {
      "id": 8,
      "key": "Vladimir",
      "name": "Vladimir",
      "title": "the Crimson Reaper"
    },
    "9": {
      "id": 9,
      "key": "Fiddlesticks",
      "name": "Fiddlesticks",
      "title": "the Harbinger of Doom"
    },
    "10": {
      "id": 10,
      "key": "Kayle",
      "name": "Kayle",
      "title": "The Judicator"
    },
    "11": {
      "id": 11,
      "key": "MasterYi",
      "name": "Master Yi",
      "title": "the Wuju Bladesman"
    },
    "12": {
      "id": 12,
      "key": "Alistar",
      "name": "Alistar",
      "title": "the Minotaur"
    },
    "13": {
      "id": 13,
      "key": "Ryze",
      "name": "Ryze",
      "title": "the Rune Mage"
    },
    "14": {
      "id": 14,
      "key": "Sion",
      "name": "Sion",
      "title": "The Undead Juggernaut"
    },
    "15": {
      "id": 15,
      "key": "Sivir",
      "name": "Sivir",
      "title": "the Battle Mistress"
    },
    "16": {
      "id": 16,
      "key": "Soraka",
      "name": "Soraka",
      "title": "the Starchild"
    },
    "17": {
      "id": 17,
      "key": "Teemo",
      "name": "Teemo",
      "title": "the Swift Scout"
    },
    "18": {
      "id": 18,
      "key": "Tristana",
      "name": "Tristana",
      "title": "the Yordle Gunner"
    },
    "19": {
      "id": 19,
      "key": "Warwick",
      "name": "Warwick",
      "title": "the Uncaged Wrath of Zaun"
    },
    "20": {
      "id": 20,
      "key": "Nunu",
      "name": "Nunu",
      "title": "the Yeti Rider"
    },
    "21": {
      "id": 21,
      "key": "MissFortune",
      "name": "Miss Fortune",
      "title": "the Bounty Hunter"
    },
    "22": {
      "id": 22,
      "key": "Ashe",
      "name": "Ashe",
      "title": "the Frost Archer"
    },
    "23": {
      "id": 23,
      "key": "Tryndamere",
      "name": "Tryndamere",
      "title": "the Barbarian King"
    },
    "24": {
      "id": 24,
      "key": "Jax",
      "name": "Jax",
      "title": "Grandmaster at Arms"
    },
    "25": {
      "id": 25,
      "key": "Morgana",
      "name": "Morgana",
      "title": "Fallen Angel"
    },
    "26": {
      "id": 26,
      "key": "Zilean",
      "name": "Zilean",
      "title": "the Chronokeeper"
    },
    "27": {
      "id": 27,
      "key": "Singed",
      "name": "Singed",
      "title": "the Mad Chemist"
    },
    "28": {
      "id": 28,
      "key": "Evelynn",
      "name": "Evelynn",
      "title": "the Widowmaker"
    },
    "29": {
      "id": 29,
      "key": "Twitch",
      "name": "Twitch",
      "title": "the Plague Rat"
    },
    "30": {
      "id": 30,
      "key": "Karthus",
      "name": "Karthus",
      "title": "the Deathsinger"
    },
    "31": {
      "id": 31,
      "key": "Chogath",
      "name": "Cho'Gath",
      "title": "the Terror of the Void"
    },
    "32": {
      "id": 32,
      "key": "Amumu",
      "name": "Amumu",
      "title": "the Sad Mummy"
    },
    "33": {
      "id": 33,
      "key": "Rammus",
      "name": "Rammus",
      "title": "the Armordillo"
    },
    "34": {
      "id": 34,
      "key": "Anivia",
      "name": "Anivia",
      "title": "the Cryophoenix"
    },
    "35": {
      "id": 35,
      "key": "Shaco",
      "name": "Shaco",
      "title": "the Demon Jester"
    },
    "36": {
      "id": 36,
      "key": "DrMundo",
      "name": "Dr. Mundo",
      "title": "the Madman of Zaun"
    },
    "37": {
      "id": 37,
      "key": "Sona",
      "name": "Sona",
      "title": "Maven of the Strings"
    },
    "38": {
      "id": 38,
      "key": "Kassadin",
      "name": "Kassadin",
      "title": "the Void Walker"
    },
    "39": {
      "id": 39,
      "key": "Irelia",
      "name": "Irelia",
      "title": "the Will of the Blades"
    },
    "40": {
      "id": 40,
      "key": "Janna",
      "name": "Janna",
      "title": "the Storm's Fury"
    },
    "41": {
      "id": 41,
      "key": "Gangplank",
      "name": "Gangplank",
      "title": "the Saltwater Scourge"
    },
    "42": {
      "id": 42,
      "key": "Corki",
      "name": "Corki",
      "title": "the Daring Bombardier"
    },
    "43": {
      "id": 43,
      "key": "Karma",
      "name": "Karma",
      "title": "the Enlightened One"
    },
    "44": {
      "id": 44,
      "key": "Taric",
      "name": "Taric",
      "title": "the Shield of Valoran"
    },
    "45": {
      "id": 45,
      "key": "Veigar",
      "name": "Veigar",
      "title": "the Tiny Master of Evil"
    },
    "48": {
      "id": 48,
      "key": "Trundle",
      "name": "Trundle",
      "title": "the Troll King"
    },
    "50": {
      "id": 50,
      "key": "Swain",
      "name": "Swain",
      "title": "the Master Tactician"
    },
    "51": {
      "id": 51,
      "key": "Caitlyn",
      "name": "Caitlyn",
      "title": "the Sheriff of Piltover"
    },
    "53": {
      "id": 53,
      "key": "Blitzcrank",
      "name": "Blitzcrank",
      "title": "the Great Steam Golem"
    },
    "54": {
      "id": 54,
      "key": "Malphite",
      "name": "Malphite",
      "title": "Shard of the Monolith"
    },
    "55": {
      "id": 55,
      "key": "Katarina",
      "name": "Katarina",
      "title": "the Sinister Blade"
    },
    "56": {
      "id": 56,
      "key": "Nocturne",
      "name": "Nocturne",
      "title": "the Eternal Nightmare"
    },
    "57": {
      "id": 57,
      "key": "Maokai",
      "name": "Maokai",
      "title": "the Twisted Treant"
    },
    "58": {
      "id": 58,
      "key": "Renekton",
      "name": "Renekton",
      "title": "the Butcher of the Sands"
    },
    "59": {
      "id": 59,
      "key": "JarvanIV",
      "name": "Jarvan IV",
      "title": "the Exemplar of Demacia"
    },
    "60": {
      "id": 60,
      "key": "Elise",
      "name": "Elise",
      "title": "the Spider Queen"
    },
    "61": {
      "id": 61,
      "key": "Orianna",
      "name": "Orianna",
      "title": "the Lady of Clockwork"
    },
    "62": {
      "id": 62,
      "key": "MonkeyKing",
      "name": "Wukong",
      "title": "the Monkey King"
    },
    "63": {
      "id": 63,
      "key": "Brand",
      "name": "Brand",
      "title": "the Burning Vengeance"
    },
    "64": {
      "id": 64,
      "key": "LeeSin",
      "name": "Lee Sin",
      "title": "the Blind Monk"
    },
    "67": {
      "id": 67,
      "key": "Vayne",
      "name": "Vayne",
      "title": "the Night Hunter"
    },
    "68": {
      "id": 68,
      "key": "Rumble",
      "name": "Rumble",
      "title": "the Mechanized Menace"
    },
    "69": {
      "id": 69,
      "key": "Cassiopeia",
      "name": "Cassiopeia",
      "title": "the Serpent's Embrace"
    },
    "72": {
      "id": 72,
      "key": "Skarner",
      "name": "Skarner",
      "title": "the Crystal Vanguard"
    },
    "74": {
      "id": 74,
      "key": "Heimerdinger",
      "name": "Heimerdinger",
      "title": "the Revered Inventor"
    },
    "75": {
      "id": 75,
      "key": "Nasus",
      "name": "Nasus",
      "title": "the Curator of the Sands"
    },
    "76": {
      "id": 76,
      "key": "Nidalee",
      "name": "Nidalee",
      "title": "the Bestial Huntress"
    },
    "77": {
      "id": 77,
      "key": "Udyr",
      "name": "Udyr",
      "title": "the Spirit Walker"
    },
    "78": {
      "id": 78,
      "key": "Poppy",
      "name": "Poppy",
      "title": "Keeper of the Hammer"
    },
    "79": {
      "id": 79,
      "key": "Gragas",
      "name": "Gragas",
      "title": "the Rabble Rouser"
    },
    "80": {
      "id": 80,
      "key": "Pantheon",
      "name": "Pantheon",
      "title": "the Artisan of War"
    },
    "81": {
      "id": 81,
      "key": "Ezreal",
      "name": "Ezreal",
      "title": "the Prodigal Explorer"
    },
    "82": {
      "id": 82,
      "key": "Mordekaiser",
      "name": "Mordekaiser",
      "title": "the Iron Revenant"
    },
    "83": {
      "id": 83,
      "key": "Yorick",
      "name": "Yorick",
      "title": "Shepherd of Souls"
    },
    "84": {
      "id": 84,
      "key": "Akali",
      "name": "Akali",
      "title": "the Fist of Shadow"
    },
    "85": {
      "id": 85,
      "key": "Kennen",
      "name": "Kennen",
      "title": "the Heart of the Tempest"
    },
    "86": {
      "id": 86,
      "key": "Garen",
      "name": "Garen",
      "title": "The Might of Demacia"
    },
    "89": {
      "id": 89,
      "key": "Leona",
      "name": "Leona",
      "title": "the Radiant Dawn"
    },
    "90": {
      "id": 90,
      "key": "Malzahar",
      "name": "Malzahar",
      "title": "the Prophet of the Void"
    },
    "91": {
      "id": 91,
      "key": "Talon",
      "name": "Talon",
      "title": "the Blade's Shadow"
    },
    "92": {
      "id": 92,
      "key": "Riven",
      "name": "Riven",
      "title": "the Exile"
    },
    "96": {
      "id": 96,
      "key": "KogMaw",
      "name": "Kog'Maw",
      "title": "the Mouth of the Abyss"
    },
    "98": {
      "id": 98,
      "key": "Shen",
      "name": "Shen",
      "title": "the Eye of Twilight"
    },
    "99": {
      "id": 99,
      "key": "Lux",
      "name": "Lux",
      "title": "the Lady of Luminosity"
    },
    "101": {
      "id": 101,
      "key": "Xerath",
      "name": "Xerath",
      "title": "the Magus Ascendant"
    },
    "102": {
      "id": 102,
      "key": "Shyvana",
      "name": "Shyvana",
      "title": "the Half-Dragon"
    },
    "103": {
      "id": 103,
      "key": "Ahri",
      "name": "Ahri",
      "title": "the Nine-Tailed Fox"
    },
    "104": {
      "id": 104,
      "key": "Graves",
      "name": "Graves",
      "title": "the Outlaw"
    },
    "105": {
      "id": 105,
      "key": "Fizz",
      "name": "Fizz",
      "title": "the Tidal Trickster"
    },
    "106": {
      "id": 106,
      "key": "Volibear",
      "name": "Volibear",
      "title": "the Thunder's Roar"
    },
    "107": {
      "id": 107,
      "key": "Rengar",
      "name": "Rengar",
      "title": "the Pridestalker"
    },
    "110": {
      "id": 110,
      "key": "Varus",
      "name": "Varus",
      "title": "the Arrow of Retribution"
    },
    "111": {
      "id": 111,
      "key": "Nautilus",
      "name": "Nautilus",
      "title": "the Titan of the Depths"
    },
    "112": {
      "id": 112,
      "key": "Viktor",
      "name": "Viktor",
      "title": "the Machine Herald"
    },
    "113": {
      "id": 113,
      "key": "Sejuani",
      "name": "Sejuani",
      "title": "Fury of the North"
    },
    "114": {
      "id": 114,
      "key": "Fiora",
      "name": "Fiora",
      "title": "the Grand Duelist"
    },
    "115": {
      "id": 115,
      "key": "Ziggs",
      "name": "Ziggs",
      "title": "the Hexplosives Expert"
    },
    "117": {
      "id": 117,
      "key": "Lulu",
      "name": "Lulu",
      "title": "the Fae Sorceress"
    },
    "119": {
      "id": 119,
      "key": "Draven",
      "name": "Draven",
      "title": "the Glorious Executioner"
    },
    "120": {
      "id": 120,
      "key": "Hecarim",
      "name": "Hecarim",
      "title": "the Shadow of War"
    },
    "121": {
      "id": 121,
      "key": "Khazix",
      "name": "Kha'Zix",
      "title": "the Voidreaver"
    },
    "122": {
      "id": 122,
      "key": "Darius",
      "name": "Darius",
      "title": "the Hand of Noxus"
    },
    "126": {
      "id": 126,
      "key": "Jayce",
      "name": "Jayce",
      "title": "the Defender of Tomorrow"
    },
    "127": {
      "id": 127,
      "key": "Lissandra",
      "name": "Lissandra",
      "title": "the Ice Witch"
    },
    "131": {
      "id": 131,
      "key": "Diana",
      "name": "Diana",
      "title": "Scorn of the Moon"
    },
    "133": {
      "id": 133,
      "key": "Quinn",
      "name": "Quinn",
      "title": "Demacia's Wings"
    },
    "134": {
      "id": 134,
      "key": "Syndra",
      "name": "Syndra",
      "title": "the Dark Sovereign"
    },
    "136": {
      "id": 136,
      "key": "AurelionSol",
      "name": "Aurelion Sol",
      "title": "The Star Forger"
    },
    "143": {
      "id": 143,
      "key": "Zyra",
      "name": "Zyra",
      "title": "Rise of the Thorns"
    },
    "150": {
      "id": 150,
      "key": "Gnar",
      "name": "Gnar",
      "title": "the Missing Link"
    },
    "154": {
      "id": 154,
      "key": "Zac",
      "name": "Zac",
      "title": "the Secret Weapon"
    },
    "157": {
      "id": 157,
      "key": "Yasuo",
      "name": "Yasuo",
      "title": "the Unforgiven"
    },
    "161": {
      "id": 161,
      "key": "Velkoz",
      "name": "Vel'Koz",
      "title": "the Eye of the Void"
    },
    "163": {
      "id": 163,
      "key": "Taliyah",
      "name": "Taliyah",
      "title": "the Stoneweaver"
    },
    "164": {
      "id": 164,
      "key": "Camille",
      "name": "Camille",
      "title": "the Steel Shadow"
    },
    "201": {
      "id": 201,
      "key": "Braum",
      "name": "Braum",
      "title": "the Heart of the Freljord"
    },
    "202": {
      "id": 202,
      "key": "Jhin",
      "name": "Jhin",
      "title": "the Virtuoso"
    },
    "203": {
      "id": 203,
      "key": "Kindred",
      "name": "Kindred",
      "title": "The Eternal Hunters"
    },
    "222": {
      "id": 222,
      "key": "Jinx",
      "name": "Jinx",
      "title": "the Loose Cannon"
    },
    "223": {
      "id": 223,
      "key": "TahmKench",
      "name": "Tahm Kench",
      "title": "the River King"
    },
    "236": {
      "id": 236,
      "key": "Lucian",
      "name": "Lucian",
      "title": "the Purifier"
    },
    "238": {
      "id": 238,
      "key": "Zed",
      "name": "Zed",
      "title": "the Master of Shadows"
    },
    "240": {
      "id": 240,
      "key": "Kled",
      "name": "Kled",
      "title": "the Cantankerous Cavalier"
    },
    "245": {
      "id": 245,
      "key": "Ekko",
      "name": "Ekko",
      "title": "the Boy Who Shattered Time"
    },
    "254": {
      "id": 254,
      "key": "Vi",
      "name": "Vi",
      "title": "the Piltover Enforcer"
    },
    "266": {
      "id": 266,
      "key": "Aatrox",
      "name": "Aatrox",
      "title": "the Darkin Blade"
    },
    "267": {
      "id": 267,
      "key": "Nami",
      "name": "Nami",
      "title": "the Tidecaller"
    },
    "268": {
      "id": 268,
      "key": "Azir",
      "name": "Azir",
      "title": "the Emperor of the Sands"
    },
    "412": {
      "id": 412,
      "key": "Thresh",
      "name": "Thresh",
      "title": "the Chain Warden"
    },
    "420": {
      "id": 420,
      "key": "Illaoi",
      "name": "Illaoi",
      "title": "the Kraken Priestess"
    },
    "421": {
      "id": 421,
      "key": "RekSai",
      "name": "Rek'Sai",
      "title": "the Void Burrower"
    },
    "427": {
      "id": 427,
      "key": "Ivern",
      "name": "Ivern",
      "title": "the Green Father"
    },
    "429": {
      "id": 429,
      "key": "Kalista",
      "name": "Kalista",
      "title": "the Spear of Vengeance"
    },
    "432": {
      "id": 432,
      "key": "Bard",
      "name": "Bard",
      "title": "the Wandering Caretaker"
    },
    "497": {
      "id": 497,
      "key": "Rakan",
      "name": "Rakan",
      "title": "The Charmer"
    },
    "498": {
      "id": 498,
      "key": "Xayah",
      "name": "Xayah",
      "title": "the Rebel"
    }
  }
}
杰西Q

你想要 foreach,它会考虑每个元素而不管它的索引:http : //php.net/manual/en/control-structures.foreach.php

foreach($championsList["data"] as $champion) {
    echo $champion["key"];
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用jQuery遍历JSON文件

来自分类Dev

使用json.simple遍历嵌套的JSON文件

来自分类Dev

使用jQuery遍历JSON文件

来自分类Dev

如何遍历json文件中的值?

来自分类Dev

您如何使用go-simplejson遍历json文件

来自分类Dev

遍历JSON文件PowerShell

来自分类Dev

与节点js遍历json文件时未定义目录

来自分类Dev

递归遍历JSON文件以提取选定的字符串

来自分类Dev

Elasticsearch-Sense-索引JSON文件?

来自分类Dev

如何遍历json文件?

来自分类Dev

使用特定的符号循环遍历JSON文件

来自分类Dev

使用PHP遍历未知的Json文件

来自分类Dev

从网站链接循环遍历多个json文件

来自分类Dev

获取JSON文件PYTHON中的值的索引

来自分类Dev

Javascript遍历JSON文件级别以填充下拉列表

来自分类Dev

遍历NodeJS中的JSON文件

来自分类Dev

如何遍历json文件?

来自分类Dev

循环遍历子目录中的json文件

来自分类Dev

遍历JSON文件

来自分类Dev

使用jQuery遍历JSON文件

来自分类Dev

如何遍历json文件中的值?

来自分类Dev

Solr无法索引JSON文件

来自分类Dev

在索引文件中显示json数据

来自分类Dev

解析JSON文件并遍历特定参数

来自分类Dev

使用ajax,PHP和Javascript遍历JSON文件

来自分类Dev

如何使用 Python 遍历 Json 文件

来自分类Dev

使用python遍历json文件以获取特定的属性值

来自分类Dev

在 Python 中使用嵌套的 for 循环遍历推文的 JSON 文件

来自分类Dev

遍历要在图表中使用的 JSON 文件