Php 从多维数组获取信息

Php 从多维数组获取信息,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,几周前我发布了这个问题,得到了一些很好的反馈,但我没有清楚地发布我的问题,也没有得到解决问题的答案。所以,我正在重做这篇文章,然后再试一次 然而,php没有很好的函数来处理深度多维数组,这使得这很困难(但并非不可能!),这很可能是因为我选择了将数组格式化为人类可读的,而不是使其“更平坦”且更易于代码处理 我的所有源代码都可以通过Github获得: 问题 我有一个随机副本生成器,它从各种json源文件生成非常丰富的数据和嵌套数据。数组是动态的,因此它的结构会发生变化,但是有些键不会发生变化(多维数

几周前我发布了这个问题,得到了一些很好的反馈,但我没有清楚地发布我的问题,也没有得到解决问题的答案。所以,我正在重做这篇文章,然后再试一次

然而,php没有很好的函数来处理深度多维数组,这使得这很困难(但并非不可能!),这很可能是因为我选择了将数组格式化为人类可读的,而不是使其“更平坦”且更易于代码处理

我的所有源代码都可以通过Github获得:

问题 我有一个随机副本生成器,它从各种json源文件生成非常丰富的数据和嵌套数据。数组是动态的,因此它的结构会发生变化,但是有些键不会发生变化(多维数组中的每个数组都会为其生成一个uid)

所以我的数组看起来有点像这样:

{"uid":"5796615f0c12c","intro":"While traveling in the wilderness, the characters fall into a sinkhole that opens beneath their feet, dropping them into the adventure location.","goal":"Retrieve a stolen item hidden in the dungeon.","starting_area":{"desc":"Circle, 40ft. diameter; one passage at each cardinal direction","width":40,"length":40,"height":10,"shape":"circle","passages":"N,E,W","doors":"S","tile":"starting_area_006.png","id":6,"uid":"5796615f0c48e","N":{"desc":"Continue straight 20ft., comes to a dead end.","width":10,"length":20,"height":10,"shape":"","passages":"","secret_chance":10,"doors":"S","tile":"passage_006.png","id":6,"secret":{"chance":15,"description":"No hidden doors found."},"uid":"5796615f0c974","from_uid":"5796615f0c48e","S":{"desc":"Wooden","width":5,"length":1,"height":10,"barred":0,"locked":0,"secret":0,"shape":"","tile":"","id":1,"uid":"5796615f0d04c","direction":"S"}},"E":{"desc":"Continue straight 20ft., then the passage turns right and continues 10ft.","width":10,"length":20,"height":10,"shape":"","passages":"","secret_chance":0,"doors":"","tile":"passage_008.png","id":8,"uid":"5796615f0d155","from_uid":"5796615f0c48e"},"W":{"desc":"Continue straight 20ft., side passage to the left, then an additional 10ft. ahead","width":10,"length":40,"height":10,"shape":"","passages":"E","secret_chance":0,"doors":"","tile":"passage_005.png","id":5,"uid":"5796615f0da1d","from_uid":"5796615f0c48e"}},"5796615f0e422":{"content":{"content":"Trap (see 'Random Traps')","monster":0,"treasure":0,"trap":1,"id":7},"desc":"Chapel where the lair's inhabitants worship","doors":"","height":10,"id":6,"length":0,"passages":"S","purpose":"Trap designed to kill or capture creatures","shape":"square","state":"Holes, floor partially collapsed","tile":"chamber_006.png","uid":"5796615f0e520","width":0,"S":{"desc":"Continue straight 20ft., comes to a dead end.","width":10,"length":20,"height":10,"shape":"","passages":"","secret_chance":0,"doors":"","tile":"passage_009.png","id":9,"uid":"5796615f0ffc7","from_uid":"5796615f0e520"}},"5796615f10c68":{"content":{"content":"Monster (random creature)","monster":1,"treasure":0,"trap":0,"id":5},"desc":"Kennel for pets or guard beasts","doors":"E","height":10,"id":9,"length":0,"monster":[{"name":"giant weasel","size":"M","rarity":"common","cr":1,"xp":400,"ac":15,"touch-ac":14,"flat-footed-ac":14,"hp":9,"FORT":3,"REF":7,"WILL":1,"init":4,"melee":"bite","melee_hit":5,"melee_dmg":"1d6+3","Str":14,"Dex":19,"Con":10,"Int":2,"Wis":13,"Cha":11,"environments":"land","treasure":0,"desc":"The predatory nature of this wolf-sized creature is revealed by its long teeth, sleekly muscular body, and belligerent hiss.","info":"<p>Like their smaller cousins, giant weasels have long, slender bodies and short legs, and come in a variety of colors from reddish brown to snowy white.<\/p>Giant weasels are active and tenacious predators, and have a reputation for being as clever as they are quick. Both highly aggressive and extremely territorial, they frequently attack creatures that are much larger than they are.<p>Giant weasels are often trained to serve as guards and mounts by various Small humanoids. Adult giant weasels can grow to be 5 feet long, stand as high as 2-1\/2 feet tall at the shoulder, and weigh up to 150 pounds.<\/p>","url":"http:\/\/www.d20pfsrd.com\/bestiary\/monster-listings\/animals\/weasel\/weasel-giant","wiki_url":"","img_url":"","age":0,"alignment":"N","job":"","id":2}],"passages":"","purpose":"Trap designed to kill or capture creatures","shape":"square","state":"Ashes, contents mostly burned","tile":"chamber_009.png","uid":"5796615f10d6b","width":0},"5796615f13e3e":{"content":{"content":"Empty Room with treasure","monster":0,"treasure":1,"trap":0,"id":10},"desc":"Kennel for pets or guard beasts","doors":"E","height":10,"id":9,"length":0,"passages":"","purpose":"Room containing a puzzle that must be solved to bypass a trap or monster","shape":"square","state":"Pristine and in original state","tile":"chamber_009.png","uid":"5796615f13f52","width":0},"5796615f14131":{"content":{"content":"Monster (pet or allied creature) guarding treasure","monster":1,"treasure":1,"trap":0,"id":4},"desc":"Cistern or well for drinking water","doors":"S","from_uid":"5796615f0e3f5","height":10,"id":7,"length":0,"monster":[{"name":"kobold","size":"S","rarity":"common","cr":0.25,"xp":100,"ac":15,"touch-ac":12,"flat-footed-ac":14,"hp":5,"FORT":2,"REF":1,"WILL":-1,"init":0,"melee":"spear","melee_hit":1,"melee_dmg":"1d6-1\/x3","Str":9,"Dex":13,"Con":10,"Int":10,"Wis":9,"Cha":8,"environments":"temperate,underground,deep forest","treasure":0,"desc":"Short, reptilian humanoid has scaled skin, a snout filled with tiny teeth, and a long tail.","info":"<p>Kobolds are creatures of the dark, found most commonly in enormous underground warrens or the dark corners of the forest where the sun is unable to reach. <\/p><p>Due to their physical similarities, kobolds loudly proclaim themselves the scions of dragonkind, destined to rule the earth beneath the wings of their great god-cousins, but most dragons have little use for the obnoxious pests.<\/p><p>While they may speak loudly of divine right and manifest destiny, kobolds are keenly aware of their own weakness. Cowards and schemers, they never fight fair if they can help it, instead setting up ambushes and double-crosses, holing up in their warrens behind countless crude but ingenious traps, or rolling over the enemy in vast, yipping hordes.<\/p><p>Kobold coloration varies even among siblings from the same egg clutch, ranging through the colors of the chromatic dragons, with red being the most common but white, green, blue, and black kobolds not unheard of.<\/p>","url":"http:\/\/www.d20pfsrd.com\/bestiary\/monster-listings\/humanoids\/kobold","wiki_url":"https:\/\/en.wikipedia.org\/wiki\/Kobold_(Dungeons_%26_Dragons)","img_url":"http:\/\/www.d20pfsrd.com\/_\/rsrc\/1452829556123\/images\/kobold.png","age":13,"alignment":"LE","job":"warrior","id":3},{"name":"bat","size":"D","rarity":"common","cr":0.125,"xp":50,"ac":16,"touch-ac":16,"flat-footed-ac":14,"hp":2,"FORT":0,"REF":4,"WILL":2,"init":2,"melee":"bite","melee_hit":6,"melee_dmg":"1d3-5","Str":1,"Dex":15,"Con":6,"Int":2,"Wis":14,"Cha":5,"environments":"temperate,forests,deserts","treasure":0,"desc":"","info":"Most bats are insectivores or fruit-eaters, but at least one species drinks blood.","url":"http:\/\/www.d20pfsrd.com\/bestiary\/monster-listings\/animals\/bat\/bat","wiki_url":"","img_url":"","age":0,"alignment":"N","job":"","id":1},{"name":"giant weasel","size":"M","rarity":"common","cr":1,"xp":400,"ac":15,"touch-ac":14,"flat-footed-ac":14,"hp":9,"FORT":3,"REF":7,"WILL":1,"init":4,"melee":"bite","melee_hit":5,"melee_dmg":"1d6+3","Str":14,"Dex":19,"Con":10,"Int":2,"Wis":13,"Cha":11,"environments":"land","treasure":0,"desc":"The predatory nature of this wolf-sized creature is revealed by its long teeth, sleekly muscular body, and belligerent hiss.","info":"<p>Like their smaller cousins, giant weasels have long, slender bodies and short legs, and come in a variety of colors from reddish brown to snowy white.<\/p>Giant weasels are active and tenacious predators, and have a reputation for being as clever as they are quick. Both highly aggressive and extremely territorial, they frequently attack creatures that are much larger than they are.<p>Giant weasels are often trained to serve as guards and mounts by various Small humanoids. Adult giant weasels can grow to be 5 feet long, stand as high as 2-1\/2 feet tall at the shoulder, and weigh up to 150 pounds.<\/p>","url":"http:\/\/www.d20pfsrd.com\/bestiary\/monster-listings\/animals\/weasel\/weasel-giant","wiki_url":"","img_url":"","age":0,"alignment":"N","job":"","id":2}],"passages":"","purpose":"Kennel for pets or guard beasts","shape":"square","state":"Stripped bare","tile":"chamber_007.png","uid":"5796615f14223","width":0},"5796615f16bc4":{"content":{"content":"Empty Room with treasure","monster":0,"treasure":1,"trap":0,"id":10},"desc":"Kitchen for food storage and preparation","doors":"","height":10,"id":10,"length":0,"passages":"N,S","purpose":"Guardroom fortified against intruders","shape":"square","state":"Ashes, contents mostly burned","tile":"chamber_010.png","uid":"5796615f16cb0","width":0,"N":{"desc":"Continue straight 20ft., side passage to the left, then an additional 10ft. ahead","width":10,"length":40,"height":10,"shape":"","passages":"E","secret_chance":0,"doors":"","tile":"passage_005.png","id":5,"uid":"5796615f1c57e","from_uid":"5796615f16cb0"},"S":{"desc":"Continue straight 20ft., door to the right, then an additional 10 ft. ahead","width":10,"length":35,"height":10,"shape":"","passages":"","secret_chance":0,"doors":"E","tile":"passage_002.png","id":2,"uid":"5796615f1f03c","from_uid":"5796615f16cb0","E":{"desc":"Stone","width":5,"length":1,"height":10,"barred":1,"locked":0,"secret":0,"shape":"","tile":"","id":5,"uid":"5796615f222d9","direction":"E"}}},"5796615f22323":{"content":{"content":"Empty Room with treasure","monster":0,"treasure":1,"trap":0,"id":10},"desc":"Banquet room for important celebrations","doors":"","height":10,"id":3,"length":0,"passages":"N,S,E,W","purpose":"Guardroom fortified against intruders","shape":"square","state":"Stripped bare","tile":"chamber_003.png","uid":"5796615f2240d","width":0},"5796615f226b8":{"content":{"content":"Monster (pet or allied creature)","monster":1,"treasure":0,"trap":0,"id":3},"desc":"Torture chamber","doors":"N","from_uid":"5796615f13e13","height":10,"id":14,"length":0,"monster":[{"name":"giant weasel","size":"M","rarity":"common","cr":1,"xp":400,"ac":15,"touch-ac":14,"flat-footed-ac":14,"hp":9,"FORT":3,"REF":7,"WILL":1,"init":4,"melee":"bite","melee_hit":5,"melee_dmg":"1d6+3","Str":14,"Dex":19,"Con":10,"Int":2,"Wis":13,"Cha":11,"environments":"land","treasure":0,"desc":"The predatory nature of this wolf-sized creature is revealed by its long teeth, sleekly muscular body, and belligerent hiss.","info":"<p>Like their smaller cousins, giant weasels have long, slender bodies and short legs, and come in a variety of colors from reddish brown to snowy white.<\/p>Giant weasels are active and tenacious predators, and have a reputation for being as clever as they are quick. Both highly aggressive and extremely territorial, they frequently attack creatures that are much larger than they are.<p>Giant weasels are often trained to serve as guards and mounts by various Small humanoids. Adult giant weasels can grow to be 5 feet long, stand as high as 2-1\/2 feet tall at the shoulder, and weigh up to 150 pounds.<\/p>","url":"http:\/\/www.d20pfsrd.com\/bestiary\/monster-listings\/animals\/weasel\/weasel-giant","wiki_url":"","img_url":"","age":0,"alignment":"N","job":"","id":2}],"passages":"","purpose":"Trap designed to kill or capture creatures","shape":"square","state":"Stripped bare","tile":"chamber_014.png","uid":"5796615f22dfe","width":0},"5796615f2b3da":{"content":{"content":"Trap (see 'Random Traps') protecting treasure","monster":0,"treasure":1,"id":8},"desc":"Training and exercise room","doors":"","height":10,"id":15,"length":0,"passages":"E,W","purpose":"Vault for holding important treasures, accessible only by locked or secret door (75 percent chance of being trapped)","shape":"square","state":"Pristine and in original state","tile":"chamber_015.png","uid":"5796615f2b715","width":0,"E":{"desc":"Stairs","width":10,"length":20,"height":10,"shape":"","passages":"","secret_chance":0,"doors":"","tile":"passage_010.png","id":10,"uid":"5796615f4013c","from_uid":"5796615f2b715"},"W":{"desc":"Continue straight 20ft., side passage to the right. then an additional 10ft. ahead","width":10,"length":40,"height":10,"shape":"","passages":"E,W","secret_chance":0,"doors":"","tile":"passage_004.png","id":4,"uid":"5796615f4637f","from_uid":"5796615f2b715"}},"5796615f4bc98":{"content":{"content":"Monster (random creature) with treasure","monster":1,"treasure":1,"trap":0,"id":6},"desc":"Barracks where the lair's defenders are quartered","doors":"E,W","height":10,"id":4,"length":0,"monster":[{"name":"bat","size":"D","rarity":"common","cr":0.125,"xp":50,"ac":16,"touch-ac":16,"flat-footed-ac":14,"hp":2,"FORT":0,"REF":4,"WILL":2,"init":2,"melee":"bite","melee_hit":6,"melee_dmg":"1d3-5","Str":1,"Dex":15,"Con":6,"Int":2,"Wis":14,"Cha":5,"environments":"temperate,forests,deserts","treasure":0,"desc":"","info":"Most bats are insectivores or fruit-eaters, but at least one species drinks blood.","url":"http:\/\/www.d20pfsrd.com\/bestiary\/monster-listings\/animals\/bat\/bat","wiki_url":"","img_url":"","age":0,"alignment":"N","job":"","id":1},{"name":"kobold","size":"S","rarity":"common","cr":0.25,"xp":100,"ac":15,"touch-ac":12,"flat-footed-ac":14,"hp":5,"FORT":2,"REF":1,"WILL":-1,"init":0,"melee":"spear","melee_hit":1,"melee_dmg":"1d6-1\/x3","Str":9,"Dex":13,"Con":10,"Int":10,"Wis":9,"Cha":8,"environments":"temperate,underground,deep forest","treasure":0,"desc":"Short, reptilian humanoid has scaled skin, a snout filled with tiny teeth, and a long tail.","info":"<p>Kobolds are creatures of the dark, found most commonly in enormous underground warrens or the dark corners of the forest where the sun is unable to reach. <\/p><p>Due to their physical similarities, kobolds loudly proclaim themselves the scions of dragonkind, destined to rule the earth beneath the wings of their great god-cousins, but most dragons have little use for the obnoxious pests.<\/p><p>While they may speak loudly of divine right and manifest destiny, kobolds are keenly aware of their own weakness. Cowards and schemers, they never fight fair if they can help it, instead setting up ambushes and double-crosses, holing up in their warrens behind countless crude but ingenious traps, or rolling over the enemy in vast, yipping hordes.<\/p><p>Kobold coloration varies even among siblings from the same egg clutch, ranging through the colors of the chromatic dragons, with red being the most common but white, green, blue, and black kobolds not unheard of.<\/p>","url":"http:\/\/www.d20pfsrd.com\/bestiary\/monster-listings\/humanoids\/kobold","wiki_url":"https:\/\/en.wikipedia.org\/wiki\/Kobold_(Dungeons_%26_Dragons)","img_url":"http:\/\/www.d20pfsrd.com\/_\/rsrc\/1452829556123\/images\/kobold.png","age":13,"alignment":"LE","job":"warrior","id":3},{"name":"giant weasel","size":"M","rarity":"common","cr":1,"xp":400,"ac":15,"touch-ac":14,"flat-footed-ac":14,"hp":9,"FORT":3,"REF":7,"WILL":1,"init":4,"melee":"bite","melee_hit":5,"melee_dmg":"1d6+3","Str":14,"Dex":19,"Con":10,"Int":2,"Wis":13,"Cha":11,"environments":"land","treasure":0,"desc":"The predatory nature of this wolf-sized creature is revealed by its long teeth, sleekly muscular body, and belligerent hiss.","info":"<p>Like their smaller cousins, giant weasels have long, slender bodies and short legs, and come in a variety of colors from reddish brown to snowy white.<\/p>Giant weasels are active and tenacious predators, and have a reputation for being as clever as they are quick. Both highly aggressive and extremely territorial, they frequently attack creatures that are much larger than they are.<p>Giant weasels are often trained to serve as guards and mounts by various Small humanoids. Adult giant weasels can grow to be 5 feet long, stand as high as 2-1\/2 feet tall at the shoulder, and weigh up to 150 pounds.<\/p>","url":"http:\/\/www.d20pfsrd.com\/bestiary\/monster-listings\/animals\/weasel\/weasel-giant","wiki_url":"","img_url":"","age":0,"alignment":"N","job":"","id":2}],"passages":"","purpose":"Vault for holding important treasures, accessible only by locked or secret door (75 percent chance of being trapped)","shape":"square","state":"Furniture wrecked but stil l present","tile":"chamber_004.png","uid":"5796615f4bf03","width":0},"5796615f5ca87":{"content":{"content":"Empty Room","monster":0,"treasure":0,"trap":0,"id":9},"desc":"Throne room where the lair's leaders hold court","doors":"","height":10,"id":13,"length":0,"passages":"S","purpose":"Trap designed to kill or capture creatures","shape":"square","state":"Pristine and in original state","tile":"chamber_013.png","uid":"5796615f5cb95","width":0,"S":{"desc":"Continue straight 20ft., door to the left, then an additional 10 ft. ahead","width":10,"length":35,"height":10,"shape":"","passages":"","secret_chance":0,"doors":"E","tile":"passage_003.png","id":3,"uid":"5796615f78c52","from_uid":"5796615f5cb95","E":{"desc":"Wooden","width":5,"length":1,"height":10,"barred":1,"locked":0,"secret":0,"shape":"","tile":"","id":2,"uid":"5796615f82df7","direction":"E"}}},"5796615f82e5f":{"content":{"content":"Monster (pet or allied creature)","monster":1,"treasure":0,"trap":0,"id":3},"desc":"Training and exercise room","doors":"","height":10,"id":15,"length":0,"monster":[{"name":"bat","size":"D","rarity":"common","cr":0.125,"xp":50,"ac":16,"touch-ac":16,"flat-footed-ac":14,"hp":2,"FORT":0,"REF":4,"WILL":2,"init":2,"melee":"bite","melee_hit":6,"melee_dmg":"1d3-5","Str":1,"Dex":15,"Con":6,"Int":2,"Wis":14,"Cha":5,"environments":"temperate,forests,deserts","treasure":0,"desc":"","info":"Most bats are insectivores or fruit-eaters, but at least one species drinks blood.","url":"http:\/\/www.d20pfsrd.com\/bestiary\/monster-listings\/animals\/bat\/bat","wiki_url":"","img_url":"","age":0,"alignment":"N","job":"","id":1},{"name":"kobold","size":"S","rarity":"common","cr":0.25,"xp":100,"ac":15,"touch-ac":12,"flat-footed-ac":14,"hp":5,"FORT":2,"REF":1,"WILL":-1,"init":0,"melee":"spear","melee_hit":1,"melee_dmg":"1d6-1\/x3","Str":9,"Dex":13,"Con":10,"Int":10,"Wis":9,"Cha":8,"environments":"temperate,underground,deep forest","treasure":0,"desc":"Short, reptilian humanoid has scaled skin, a snout filled with tiny teeth, and a long tail.","info":"<p>Kobolds are creatures of the dark, found most commonly in enormous underground warrens or the dark corners of the forest where the sun is unable to reach. <\/p><p>Due to their physical similarities, kobolds loudly proclaim themselves the scions of dragonkind, destined to rule the earth beneath the wings of their great god-cousins, but most dragons have little use for the obnoxious pests.<\/p><p>While they may speak loudly of divine right and manifest destiny, kobolds are keenly aware of their own weakness. Cowards and schemers, they never fight fair if they can help it, instead setting up ambushes and double-crosses, holing up in their warrens behind countless crude but ingenious traps, or rolling over the enemy in vast, yipping hordes.<\/p><p>Kobold coloration varies even among siblings from the same egg clutch, ranging through the colors of the chromatic dragons, with red being the most common but white, green, blue, and black kobolds not unheard of.<\/p>","url":"http:\/\/www.d20pfsrd.com\/bestiary\/monster-listings\/humanoids\/kobold","wiki_url":"https:\/\/en.wikipedia.org\/wiki\/Kobold_(Dungeons_%26_Dragons)","img_url":"http:\/\/www.d20pfsrd.com\/_\/rsrc\/1452829556123\/images\/kobold.png","age":13,"alignment":"LE","job":"warrior","id":3},{"name":"bat","size":"D","rarity":"common","cr":0.125,"xp":50,"ac":16,"touch-ac":16,"flat-footed-ac":14,"hp":2,"FORT":0,"REF":4,"WILL":2,"init":2,"melee":"bite","melee_hit":6,"melee_dmg":"1d3-5","Str":1,"Dex":15,"Con":6,"Int":2,"Wis":14,"Cha":5,"environments":"temperate,forests,deserts","treasure":0,"desc":"","info":"Most bats are insectivores or fruit-eaters, but at least one species drinks blood.","url":"http:\/\/www.d20pfsrd.com\/bestiary\/monster-listings\/animals\/bat\/bat","wiki_url":"","img_url":"","age":0,"alignment":"N","job":"","id":1},{"name":"giant weasel","size":"M","rarity":"common","cr":1,"xp":400,"ac":15,"touch-ac":14,"flat-footed-ac":14,"hp":9,"FORT":3,"REF":7,"WILL":1,"init":4,"melee":"bite","melee_hit":5,"melee_dmg":"1d6+3","Str":14,"Dex":19,"Con":10,"Int":2,"Wis":13,"Cha":11,"environments":"land","treasure":0,"desc":"The predatory nature of this wolf-sized creature is revealed by its long teeth, sleekly muscular body, and belligerent hiss.","info":"<p>Like their smaller cousins, giant weasels have long, slender bodies and short legs, and come in a variety of colors from reddish brown to snowy white.<\/p>Giant weasels are active and tenacious predators, and have a reputation for being as clever as they are quick. Both highly aggressive and extremely territorial, they frequently attack creatures that are much larger than they are.<p>Giant weasels are often trained to serve as guards and mounts by various Small humanoids. Adult giant weasels can grow to be 5 feet long, stand as high as 2-1\/2 feet tall at the shoulder, and weigh up to 150 pounds.<\/p>","url":"http:\/\/www.d20pfsrd.com\/bestiary\/monster-listings\/animals\/weasel\/weasel-giant","wiki_url":"","img_url":"","age":0,"alignment":"N","job":"","id":2}],"passages":"E,W","purpose":"Antechamber or waiting room for spectators","shape":"square","state":"Ashes, contents mostly burned","tile":"chamber_015.png","uid":"5796615f83005","width":0,"E":{"desc":"Continue straight 20ft., door to the left, then an additional 10 ft. ahead","width":10,"length":35,"height":10,"shape":"","passages":"","secret_chance":0,"doors":"E","tile":"passage_003.png","id":3,"uid":"5796615f94287","from_uid":"5796615f83005","E":{"desc":"Stone","width":5,"length":1,"height":10,"barred":1,"locked":0,"secret":0,"shape":"","tile":"","id":5,"uid":"5796615f9c71d","direction":"E"}},"W":{"desc":"Continue straight 30ft., no doors or side passages","width":10,"length":30,"height":10,"shape":"","passages":"","secret_chance":0,"doors":"","tile":"passage_001.png","id":1,"uid":"5796615f9c819","from_uid":"5796615f83005"}}}
插入数组(给定搜索返回位置的$path、要插入多维数组的数组以及添加数组时获得的标签)


希望这能更好地解释这个问题。谢谢你的时间

我认为最好的方法是使用json_decode()将json格式化为数组

然后循环遍历数组,检查uid是否与所需的值匹配,如果匹配,则获取结果

像这样的东西可能会有帮助

$array[$i]->starting_area[$j]->N[$k]->passage

可能会为您提供所需的值

我认为最好的方法是使用json_decode()将json格式化为数组

然后循环遍历数组,检查uid是否与所需的值匹配,如果匹配,则获取结果

像这样的东西可能会有帮助

$array[$i]->starting_area[$j]->N[$k]->passage

可能会为您提供所需的值首先,您可能需要检查您发布的
json
字符串。。。它可能没有正确格式化,或者它的某些部分可能没有正确复制和粘贴。在任何情况下,数据都是为了在这篇文章中使用而重建的

把这一观察抛在脑后;下面是一个有注释的函数,可以帮助您做到这一点。。。(返回“通道”的值,其中uid=5790de65145bc)。该函数接受2个参数。第一个参数是
JSON字符串
,第二个参数是要获取段落的
uid
。你不妨试试看


首先,您可能需要检查您发布的
json
字符串。。。它可能没有正确格式化,或者它的某些部分可能没有正确复制和粘贴。在任何情况下,数据都是为了在这篇文章中使用而重建的

把这一观察抛在脑后;下面是一个有注释的函数,可以帮助您做到这一点。。。(返回“通道”的值,其中uid=5790de65145bc)。该函数接受2个参数。第一个参数是
JSON字符串
,第二个参数是要获取段落的
uid
。你不妨试试看


您对数据结构的控制程度如何?UID似乎是一个很好的候选键,而不仅仅是集合中的另一个元素。我完全可以控制它,它只是我个人的学习项目。UID只是一种为东西提供唯一ID的方法,这样我就可以针对数组的某些部分(我在这里遇到了麻烦),我试图让它成为人类可读的,事实证明这样做很难。你说得对,我试图修复它,但遇到了麻烦。我的项目实际上在github上:有完整的数据文件。我希望重做这个条目,并澄清我的数据是动态的,因此我需要一种灵活的方法来查找数据。您对数据的结构有多大的控制?UID似乎是一个很好的候选键,而不仅仅是集合中的另一个元素。我完全可以控制它,它只是我个人的学习项目。UID只是一种为东西提供唯一ID的方法,这样我就可以针对数组的某些部分(我在这里遇到了麻烦),我试图让它成为人类可读的,事实证明这样做很难。你说得对,我试图修复它,但遇到了麻烦。我的项目实际上在github上:有完整的数据文件。我希望重做这个条目,并澄清我的数据是动态的,因此我需要一种灵活的方法来查找数据。json是有效的(我使用jsonlint和内部json检查程序来验证外部json数据的格式),我可能发布了一个错误的片段…(很可能)。我会尝试你的样品,并尽快回复谢谢$iPassages=getPassagesByUID($_SESSION['dungeon',$GenerateQueue[0]);其中$GenerateQueue[0]=5790de65145bc和$U会话['dungeon']是上述数组,返回null。当我回显返回的$key值时,我得到:uid intro goal start_区域-它没有进入数组,只是停留在表面级别…:-(@DavidWhite会再看一次……现在知道问题可能在哪里了be@DavidWhite但是,再次检查它,要非常小心,
生成的JSON数据
不是原始数据,而是一个
JSON字符串
,否则它将不起作用……无论如何,您也可以在这里测试它:尝试一下,产生一些提示和1个致命错误。。让我调整一下。不喜欢:$passion=$data->N->passion;注意:尝试获取非对象的属性,我会尝试调整它。哦,我删除了json解码,因为我不需要它,因为此时我的数据是$\u会话数组。我不应该说json数据…因为它不在这个阶段。json是有效的(我使用jsonlint和内部json检查程序来验证我的外部json数据的格式),我可能发布了一个错误的代码片段…(很可能)。我将尝试您的示例并尽快回复,谢谢!!$iPassages=getPassagesByUID($_SESSION['dungeon'],$GenerateQueue[0]);其中$GenerateQueue[0]=5790de65145bc和$_SESSION['dungeon']是上面的数组,r
$array = json_decode($json_string) // Considering json_string holds your json array
$array[$i]->starting_area[$j]->N[$k]->passage
<?php

    // THIS MUST BE A JSON STRING... NOT AN ARRAY OR PHP OBJECT...
    // HOWEVER, IF YOU ARE GENERATING AN A PHP OBJECT (AND NOT JSON STRING)
    // THE FUNCTION WOULD STILL WORK BUT YOU HAVE TO 
    // SIMPLY NOT DO A json_decode WITHIN THE FUNCTION
    $strJson    = '{"uid":"5790de6513913","intro":"While traveling in the wilderness, the characters fall into a sinkhole that opens beneath their feet, dropping them into the adventure location.","goal":"Retrieve a stolen item hidden in the dungeon.","starting_area":{"desc":"Circle, 40ft. diameter; one passage at each cardinal direction","width":40,"length":40,"height":10,"shape":"circle","passages":"N,E,W","doors":"S","tile":"starting_area_006.png","id":6,"uid":"5790de6513da8","N":{"passage":{"desc":"Continue straight 20ft., comes to a dead end.","width":10,"length":20,"height":10,"shape":"","passages":"211","secret_chance":10,"doors":"S","tile":"passage_006.png","id":6,"uid":"5790de65145bc","from_uid":"5790de6513da8","secret":{"chance":15,"description":"No hidden doors found."}}}}}';

    $iPassages  = getPassagesByUID($strJson, "5790de65145bc");

    var_dump($iPassages);     //<== DISPLAYS:: int 211


    function getPassagesByUID($jsonString, $uid){
        // DECODE/CONVERT THE $jsonString INTO A PHP OBJECT.
        // IF $jsonString IS ALREADY A PHP OBJECT, YOU DON'T HAVE TO json_decode IT;
        // INSTEAD THE LINE BELOW SHOULD READ:
        // $objJson = $jsonString;  //SO BE CAREFUL WHAT YOU PASS IN ;-)
        $objJson    = json_decode($jsonString);

        // LOOP THROUGH ALL THE ENTRIES IN THE JSON OBJECT
        foreach($objJson as $key=>$data){
            // CHECK IF THE PROPERTY IS starting_area
            // (SINCE starting_area CONTAINS THE N OBJECT WHICH CONTAINS  THE PASSAGE OBJECT)
            if($key == "starting_area"){
                // IF THAT IS THE CASE, GET THE passage OBJECT & STORE IT IN A VARIABLE
                $passage = $data->N->passage;
                // CHECK IF THE PROPERTY passages EXIST ON THIS PASSAGE OBJECT
                // AND THAT THE uid PROPERTY MATCHES THE $uid GIVEN AS A PARAMETER
                if(property_exists($passage, "passages") && $passage->uid == $uid){
                    // IF IT DOES, RETURN THE VALUE THEREOF
                    return $passage->passages;
                }
            }
        }
        return null;
    }
<?php

    $jsonString = '{
        "uid"           : "5790de6513913",
        "intro"         : "While traveling in the wilderness, the characters fall into a sinkhole that opens beneath their feet, dropping them into the adventure location.",
        "goal"          : "Retrieve a stolen item hidden in the dungeon.",
        "starting_area" : {
            "desc"      : "Circle, 40ft. diameter; one passage at each cardinal direction",
            "width"     : 40,
            "length"    : 40,
            "height"    : 10,
            "shape"     : "circle",
            "passages"  : "N,E,W",
            "doors"     : "S",
            "tile"      : "starting_area_006.png",
            "id"        : 6,
            "uid"       : "5790de6513da8",
            "N"         : {
                "passage": {
                    "desc"          : "Continue straight 20ft., comes to a dead end.",
                    "width"         : 10,
                    "length"        : 20,
                    "height"        : 10,
                    "shape"         : "",
                    "passages"      : "211",
                    "secret_chance" : 10,
                    "doors"         : "S",
                    "tile"          : "passage_006.png",
                    "id"            : 6,
                    "uid"           : "5790de65145bc",
                    "from_uid"      : "5790de6513da8",
                    "secret"        : {
                        "chance"        : 15,
                        "description"   : "No hidden doors found."
                    }
                }
            }
        }
    }';