jkkenzie Posted July 8, 2013 Share Posted July 8, 2013 I have the following arrays: //$this->links is an array with the following Array ( [-1] => Array ( [text] => Home [url] => pages/HOME.htm ) [1] => Array ( [text] => About us [url] => pages/About_us.htm ) [2] => Array ( [text] => Services [url] => pages/Services.htm ) [3] => Array ( [text] => Portfolio [url] => pages/Portfolio.htm ) [4] => Array ( [text] => FAQs [url] => pages/FAQs.htm ) [5] => Array ( [text] => Contact Us [url] => pages/Contact_Us.htm ) ) //$this->PrItems is an array with the following Array ( [1] => 886_P [2] => 962_P ) //$this->chiItems is an array with the following Array ( [1] => Array ( [886_P] => Array ( [959_P] => Array ( [text] => Overview [url] => pages/Overview.htm ) [960_P] => Array ( [text] => Directors [url] => pages/Directors.htm ) [961_P] => Array ( [text] => Representatives [url] => pages/Representatives.htm ) ) ) [2] => Array ( [962_P] => Array ( [965_P] => Array ( [text] => Custom Designed Homes [url] => pages/Custom_Designed_Homes.htm ) [966_P] => Array ( [text] => Pre-Designed Homes [url] => pages/Pre-Designed_Homes.htm ) [967_P] => Array ( [text] => Ready to Occupy [url] => pages/Ready_to_Occupy.htm ) ) ) ) To get to [text] => Directors i get backwards as follows: [960_P] then [886_P] then [1] in $this->PrItems THEN match [1] (parent above ) with [1] in $this->links to get [text] => About us I wanted to achieve a result like (using above): About Us .:. Directors Like a Breadcrumb What i have tried is: foreach($this->links as $Lnkkey=>$val) { if(strtolower($val['text']) == $page) { //$ParentId=$Lnkkey; echo '<li class="active"><a href="#" onclick="return false;" >'.@$val['text'].'</a></li>'; } else { $breadcrumb='<li class="active"><a href="#" onclick="return false;" >'.@$val['text'].'</a></li>'; $parentkey = find_parent($this->chItems, $page); echo $parentkey; } } function find_parent($array, $needle, $parent = null) { foreach ($array as $key => $value) { if (is_array($value)) { $pass = $parent; if (is_string($key)) { $pass = $key; } $found = find_parent($value, $needle, $pass); if ($found !== false) { return $found; } } else if ($key === 'text' && $value === $needle) { return $parent; } } return false; } thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/279969-find-successive-parents-details-recursively-in-multidimentional-array/ Share on other sites More sharing options...
jkkenzie Posted July 8, 2013 Author Share Posted July 8, 2013 class RecursiveArrayOnlyIterator extends RecursiveArrayIterator { public function hasChildren() { return is_array($this->current()); } } $iterator = new RecursiveArrayIterator($chItems); iterator_apply($iterator, 'traverseStructure', array($iterator)); function traverseStructure($iterator) { while ( $iterator -> valid() ) { if ( $iterator -> hasChildren() ) { // print all children foreach ($iterator->getChildren() as $key => $value) { echo $iterator->key(). ', '.$key . ', ' . $value ; } traverseStructure($iterator -> getChildren()); } else { //echo $iterator -> key() . ' : ' . $iterator -> current() .PHP_EOL; } $iterator -> next(); } } I have tried this but i cant match the $value with a GET variable and also get its descendant's keys.. Quote Link to comment https://forums.phpfreaks.com/topic/279969-find-successive-parents-details-recursively-in-multidimentional-array/#findComment-1439962 Share on other sites More sharing options...
jcbones Posted July 9, 2013 Share Posted July 9, 2013 There is a lot of info missing from your logic. Like: 1. How do you know what page you are on? Are you passing the index to the page somehow? 2. Why are you using 3 different arrays, instead of one multi-dem? You can do a recursive search on a single multi-dem to find which array the page is in. 3. Why not store them in a database, with a foreign key, then pull all of the parents from the foreign key? Quote Link to comment https://forums.phpfreaks.com/topic/279969-find-successive-parents-details-recursively-in-multidimentional-array/#findComment-1439990 Share on other sites More sharing options...
jkkenzie Posted July 13, 2013 Author Share Posted July 13, 2013 I got the arrays by using print_r() but the values come from the database. I did get the page name by exploding url from $SERVER... and got the text before the .(dot) somthing e.g. services.htm the page name = "services" then i use that to access its ID from the Database and get its parent (infact am using a single mysql query to do that) BUT there are pages that load from modules and not from pages table.... these are pages generated during runtime.... am yet to figure out how to fix that. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/279969-find-successive-parents-details-recursively-in-multidimentional-array/#findComment-1440608 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.