从多维数组获取信息php

时间:2016-07-21 14:48:31

标签: php arrays multidimensional-array

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

然而,php没有很好的功能来处理深度多维数组,这使得这很困难(但并非不可能!),而且很可能是因为我在格式化数组时做出的选择是人类可读的反对使它“更平坦”,更容易通过代码处理。

我的所有源代码都可以通过Github获得:https://github.com/mudgw2/dungeon-generator3

问题 我有一个随机的地下城生成器,可以生成非常丰富的数据和来自各种json源文件的非常嵌套的数据。数组是DYNAMIC,因此它的结构会发生变化,但是有些键不会改变(多维数组中的每个数组都有一个为它生成的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"}}}

有了这些数据,我希望能够开发一个函数,我可以传递一个UID值并获取该数组段中的所有信息。当生成地牢我正在通过UID查找,然后检查兄弟键(不改变,即通道和门),看看是否有任何并生成它们。

最终,程序正在遍历数组并生成缺失的内容,直到所有内容都满意为止。

为了允许我使用数组(最后将json保存到文件中),我有几个生成东西的函数,然后是两个在数组中查找数据的函数(这是我的问题),以及插入到数组中的函数(如果给定正确的路径,则可以正常工作)。

搜索动态多维数组(给它数组和UID键并返回匹配的整个数组段)

function search($arr, $key){
$ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
$results = [];
foreach ($ritit as $leafValue) {
$path = [];
foreach (range(0, $ritit->getDepth()) as $depth) {
    $path[] = $ritit->getSubIterator($depth)->key();
}
    $results[] = join('_', $path);
}
array_pop($path);
return $path;}

插入数组(给定搜索返回位置的$ path,插入多维数组的数组,以及添加时数组得到的标签)

function insert_into_array($path,$a2,$label){
$dest = &$_SESSION['dungeon'];
foreach($path as $pathSegment) {
    $dest = &$dest[$pathSegment];
}
$dest[$label] = $a2;
return true;}

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

2 个答案:

答案 0 :(得分:0)

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

$array = json_decode($json_string) // Considering json_string holds your json array

然后遍历数组并检查uid是否与您想要的值匹配,然后在匹配时获取结果

这样的事可能会有所帮助

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

可能会为您提供所需的值

答案 1 :(得分:0)

首先,您可能需要检查您发布的json字符串...它可能没有正确格式化,或者某些部分可能未被复制&amp;粘贴得当。在任何情况下,数据都被重建以用于本帖子。

将观察结果排除在外;下面是一个评论函数来帮助你做到这一点...(返回&#34的值;段落&#34;其中uid = 5790de65145bc )。该函数需要2个参数。第一个参数是JSON String,第二个参数是uid你想得到的段落。您可能还想测试here

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

有效的JSON STRING应该看起来像这样:

<?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."
                    }
                }
            }
        }
    }';

请注意,它被单引号标记包围,因此它是一个字符串....您生成的JSON应该看起来像......如果您的数据是数组或对象,并且您想将其转换为JSON格式,你可以使用:json_encode($arrayOrObject);