recursively search a hierarchial array and return all ancestors of found child, not just child |
[eluser]CroNiX[/eluser]
Hi, I have a multidimensional array where each array has a parent id node to create a hierarchical tree. Code: $hierarchy[] = array('id' => 1, 'parent_id' => 0, 'name' = 'root1'); I'm trying to come up with a recursive key/value search routine that will return all ancestor arrays of the found item without knowing the depth of the tree. All nodes with 0 for the parent_id are root level nodes. Basically I want to search for something like "where key = name and value = xxx" and have it return all ancestors of that node. So if I wanted to search for "key = name and value = root1-1", it should return and array like: Code: array[0] = array('id' => 1, 'parent_id' => 0, 'name' = 'root1'); //parent node first Code: array[0] = array('id' => 1, 'parent_id' => 0, 'name' = 'root1'); //parent node first Thanks!
[eluser]WanWizard[/eluser]
Since you are talking about a where clause, I assume you want a query? In that case, you should check out http://dev.mysql.com/tech-resources/arti...-data.html. Alternatively, if the list is not too big, you can read it all into an array, and use a recusive function to retrieve the required list.
[eluser]CroNiX[/eluser]
[quote author="WanWizard" date="1282265324"]Since you are talking about a where clause, I assume you want a query? In that case, you should check out http://dev.mysql.com/tech-resources/arti...-data.html. Alternatively, if the list is not too big, you can read it all into an array, and use a recusive function to retrieve the required list.[/quote] It is already retrieved from the db and in array form. And as far as a "where" clause it would just be something like: Code: if($array[$search_key] == $search_value) //found the node Thanks.
[eluser]bretticus[/eluser]
I've already spent too much time in this forum today (seems to be my escape from real work.) I have nothing to offer right now (may look at this tonight again and try to help.) Except, consider alternatives to recursion when possible. Iteration often solves the problem and you'll get much better performance when data is deeply nested or there is a lot of data. (Wow, sorry. I'll come back for sure tonight and offer a suggestion or two. Now I feel obligated after this post.)
[eluser]CroNiX[/eluser]
I'd appreciate it. I have a way to do it manually and only if I know how deep the tree is, but I want something generic that will work for any tree size. This is the current working code that I'm trying to update, and in this case I'm searching for the array key 'full_url', although I want to make the key searchable as well as the value. Right now Im just searching for the value. This is a little more confusing than my above example because its for a specific case. But as you can see, here is the basic flow. 1) search first parent, if found, store result as first entry in found array. 2) parent not found, go to next level if exists(children of parent). If found here, create parent entry from #1 as first entry in array, put this match second. 3) child not found, go to next level if exists (grandchildren). If found here, create parent entry from #1 as first entry in array, put child from #2 as second entry in array, put this third. 4) Continue searching branch...if not found in this branch, move to next parent and start over Also, in this case, 'nav_id' is the same as 'id' in my original example. Code: function get_location($source_array, $search_value)
[eluser]CroNiX[/eluser]
This seems to work: Code: $hierarchy[] = array('id' => 1, 'parent_id' => 0, 'name' => 'root1'); $a = find_pat($hierarchy, 'root1-1-1'); echo '<pre>'; print_r($a); correctly produces Code: Array
[eluser]WanWizard[/eluser]
Or without a recusive function: Code: // storage for the results
[eluser]CroNiX[/eluser]
Thanks for working on it also. Correct me if Im wrong, but without using a recursive function you can't use this approach where the depth of the tree is unknown.
[eluser]mddd[/eluser]
That's not correct. WanWizard's code keeps going until the top (parent==0) is reached. This is because of the " while ( $node['parent_id'] != 0 ) " loop.
[eluser]mddd[/eluser]
My thought was: why not get the information simply from the database. I assume there is a table with the fields 'id' and 'parent_id' as given in the first post. Then you could simply get the list of "parents" by this sql statement: Code: SELECT t1.id id1, t2.id id2, t3.id id3, t4.id id4 In CI you could very simply build such a query: Code: $this->db->select('t1.id id1'); |
Welcome Guest, Not a member yet? Register Sign In |