php - Traverse through unlimited nested arrays -
i trying work out way value "title" array nested @ unlimited depth in other arrays.
i realise need separate function having problem finding out how display values downwards chain can formulate other function.
the function told how many levels go down, looking in "child" element next array.
$new_map=array(); $new_map[0]["title"]="level zero"; $new_map[0]["child"]=""; $new_map1=array(); $new_map1[0]["title"]="level one"; $new_map1[0]["child"]="empty1"; $new_map2=array(); $new_map2[0]["title"]="level two"; $new_map2[0]["child"]="empty2"; $new_map3=array(); $new_map3[0]["title"]="level three"; $new_map3[0]["child"]="empty3"; $new_map[0]["child"]=$new_map1; $new_map1[0]["child"]=$new_map2; $new_map2[0]["child"]=$new_map3; foreach($new_map $key => $val){ foreach($new_map[$key]["child"] $key2 => $val2){ foreach($new_map[$key]["child"][$key2]["child"] $key3 => $val3){ echo $new_map[$key]["child"][$key2]["child"][$key3]["title"]."<br/>"; } } }
you should utilize depth first search to value "title" array nested @ unlimited depth in other arrays. instead of taking new array everytime, take 3d array
$new_map[i][0]["child"] indicates ith array.
then using dfs $new_map[i][0]["child"], check kid array $new_map[i + 1][0]["child"] if contains value title queried.
php
No comments:
Post a Comment