Getting information from multidimensional arrays php -


i posted issue few weeks ago, , got excellent feedback didn't post question , wasn't able answer solve problem. so, i'm redoing post , trying again.

however, php dosn't have functions working deep multidimensional arrays , makes difficult (but not impossible!), , due choices made in formatting array human readable opposed making "flatter" , easier handle code.

all source code available via github: https://github.com/mudgw2/dungeon-generator3

the problem have random dungeon generator thats producing rich data , nested data various json source files. array dynamic structure changes, there keys not change (each array in multidimensional array has uid generated it).

so array looks bit this:

{"uid":"5796615f0c12c","intro":"while traveling in wilderness, characters fall sinkhole opens beneath feet, dropping them adventure location.","goal":"retrieve stolen item hidden in dungeon.","starting_area":{"desc":"circle, 40ft. diameter; 1 passage @ 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 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., passage turns right , 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 left, 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 lair's inhabitants worship","doors":"","height":10,"id":6,"length":0,"passages":"s","purpose":"trap designed 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 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 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 wolf-sized creature revealed long teeth, sleekly muscular body, , belligerent hiss.","info":"<p>like smaller cousins, giant weasels have long, slender bodies , short legs, , come in variety of colors reddish brown snowy white.<\/p>giant weasels active , tenacious predators, , have reputation being clever quick. both highly aggressive , extremely territorial, attack creatures larger are.<p>giant weasels trained serve guards , mounts various small humanoids. adult giant weasels can grow 5 feet long, stand high 2-1\/2 feet tall @ shoulder, , weigh 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 kill or capture creatures","shape":"square","state":"ashes, contents burned","tile":"chamber_009.png","uid":"5796615f10d6b","width":0},"5796615f13e3e":{"content":{"content":"empty room treasure","monster":0,"treasure":1,"trap":0,"id":10},"desc":"kennel pets or guard beasts","doors":"e","height":10,"id":9,"length":0,"passages":"","purpose":"room containing puzzle must solved bypass trap or monster","shape":"square","state":"pristine , 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 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, snout filled tiny teeth, , long tail.","info":"<p>kobolds creatures of dark, found commonly in enormous underground warrens or dark corners of forest sun unable reach. <\/p><p>due physical similarities, kobolds loudly proclaim scions of dragonkind, destined rule earth beneath wings of great god-cousins, dragons have little use obnoxious pests.<\/p><p>while may speak loudly of divine right , manifest destiny, kobolds keenly aware of own weakness. cowards , schemers, never fight fair if can it, instead setting ambushes , double-crosses, holing in warrens behind countless crude ingenious traps, or rolling on enemy in vast, yipping hordes.<\/p><p>kobold coloration varies among siblings same egg clutch, ranging through colors of chromatic dragons, red being common white, green, blue, , 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 insectivores or fruit-eaters, @ least 1 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 wolf-sized creature revealed long teeth, sleekly muscular body, , belligerent hiss.","info":"<p>like smaller cousins, giant weasels have long, slender bodies , short legs, , come in variety of colors reddish brown snowy white.<\/p>giant weasels active , tenacious predators, , have reputation being clever quick. both highly aggressive , extremely territorial, attack creatures larger are.<p>giant weasels trained serve guards , mounts various small humanoids. adult giant weasels can grow 5 feet long, stand high 2-1\/2 feet tall @ shoulder, , weigh 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 pets or guard beasts","shape":"square","state":"stripped bare","tile":"chamber_007.png","uid":"5796615f14223","width":0},"5796615f16bc4":{"content":{"content":"empty room treasure","monster":0,"treasure":1,"trap":0,"id":10},"desc":"kitchen food storage , preparation","doors":"","height":10,"id":10,"length":0,"passages":"n,s","purpose":"guardroom fortified against intruders","shape":"square","state":"ashes, contents burned","tile":"chamber_010.png","uid":"5796615f16cb0","width":0,"n":{"desc":"continue straight 20ft., side passage left, 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 right, 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 treasure","monster":0,"treasure":1,"trap":0,"id":10},"desc":"banquet room 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 wolf-sized creature revealed long teeth, sleekly muscular body, , belligerent hiss.","info":"<p>like smaller cousins, giant weasels have long, slender bodies , short legs, , come in variety of colors reddish brown snowy white.<\/p>giant weasels active , tenacious predators, , have reputation being clever quick. both highly aggressive , extremely territorial, attack creatures larger are.<p>giant weasels trained serve guards , mounts various small humanoids. adult giant weasels can grow 5 feet long, stand high 2-1\/2 feet tall @ shoulder, , weigh 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 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 , exercise room","doors":"","height":10,"id":15,"length":0,"passages":"e,w","purpose":"vault holding important treasures, accessible locked or secret door (75 percent chance of being trapped)","shape":"square","state":"pristine , 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 right. 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) treasure","monster":1,"treasure":1,"trap":0,"id":6},"desc":"barracks lair's defenders 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 insectivores or fruit-eaters, @ least 1 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, snout filled tiny teeth, , long tail.","info":"<p>kobolds creatures of dark, found commonly in enormous underground warrens or dark corners of forest sun unable reach. <\/p><p>due physical similarities, kobolds loudly proclaim scions of dragonkind, destined rule earth beneath wings of great god-cousins, dragons have little use obnoxious pests.<\/p><p>while may speak loudly of divine right , manifest destiny, kobolds keenly aware of own weakness. cowards , schemers, never fight fair if can it, instead setting ambushes , double-crosses, holing in warrens behind countless crude ingenious traps, or rolling on enemy in vast, yipping hordes.<\/p><p>kobold coloration varies among siblings same egg clutch, ranging through colors of chromatic dragons, red being common white, green, blue, , 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 wolf-sized creature revealed long teeth, sleekly muscular body, , belligerent hiss.","info":"<p>like smaller cousins, giant weasels have long, slender bodies , short legs, , come in variety of colors reddish brown snowy white.<\/p>giant weasels active , tenacious predators, , have reputation being clever quick. both highly aggressive , extremely territorial, attack creatures larger are.<p>giant weasels trained serve guards , mounts various small humanoids. adult giant weasels can grow 5 feet long, stand high 2-1\/2 feet tall @ shoulder, , weigh 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 holding important treasures, accessible locked or secret door (75 percent chance of being trapped)","shape":"square","state":"furniture wrecked 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 lair's leaders hold court","doors":"","height":10,"id":13,"length":0,"passages":"s","purpose":"trap designed kill or capture creatures","shape":"square","state":"pristine , in original state","tile":"chamber_013.png","uid":"5796615f5cb95","width":0,"s":{"desc":"continue straight 20ft., door left, 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 , 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 insectivores or fruit-eaters, @ least 1 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, snout filled tiny teeth, , long tail.","info":"<p>kobolds creatures of dark, found commonly in enormous underground warrens or dark corners of forest sun unable reach. <\/p><p>due physical similarities, kobolds loudly proclaim scions of dragonkind, destined rule earth beneath wings of great god-cousins, dragons have little use obnoxious pests.<\/p><p>while may speak loudly of divine right , manifest destiny, kobolds keenly aware of own weakness. cowards , schemers, never fight fair if can it, instead setting ambushes , double-crosses, holing in warrens behind countless crude ingenious traps, or rolling on enemy in vast, yipping hordes.<\/p><p>kobold coloration varies among siblings same egg clutch, ranging through colors of chromatic dragons, red being common white, green, blue, , 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 insectivores or fruit-eaters, @ least 1 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 wolf-sized creature revealed long teeth, sleekly muscular body, , belligerent hiss.","info":"<p>like smaller cousins, giant weasels have long, slender bodies , short legs, , come in variety of colors reddish brown snowy white.<\/p>giant weasels active , tenacious predators, , have reputation being clever quick. both highly aggressive , extremely territorial, attack creatures larger are.<p>giant weasels trained serve guards , mounts various small humanoids. adult giant weasels can grow 5 feet long, stand high 2-1\/2 feet tall @ shoulder, , weigh 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 spectators","shape":"square","state":"ashes, contents burned","tile":"chamber_015.png","uid":"5796615f83005","width":0,"e":{"desc":"continue straight 20ft., door left, 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"}}} 

with data, i'm hoping able develop function can pass uid value , information in array segment. when generating dungeon i'm looking uid, checking sibling keys (which not change, ie passages , doors), see if there , generate those.

ultimately, program going on array , generating content thats missing until satisfied.

to allow me work array(which saves @ end json file), have several functions generate things, , 2 functions find data in array (which issue), , function insert array (which works if given proper path).

searching dynamic multidimensional array (give array, , uid key , return entire array segment matches)

function search($arr, $key){ $ritit = new recursiveiteratoriterator(new recursivearrayiterator($arr)); $results = []; foreach ($ritit $leafvalue) { $path = []; foreach (range(0, $ritit->getdepth()) $depth) {     $path[] = $ritit->getsubiterator($depth)->key(); }     $results[] = join('_', $path); } array_pop($path); return $path;} 

inserting array (given $path location returned search, array insert multidimensional array, , label array gets when added)

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

hopefully better explains issue. time!

i think best possible way out here json formatted array using json_decode()

$array = json_decode($json_string) // considering json_string holds json array 

then loop through array , check if uid matches desired value , fetch result if matches

something might help

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

might give desired values


Comments