![]() |
Little help with recursive function plz - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Little help with recursive function plz (/showthread.php?tid=42304) |
Little help with recursive function plz - El Forum - 06-02-2011 [eluser]Christophe28[/eluser] Hi, I'm trying to write a recursive function where the results ($item->id and $item->name) are returned in a multidimensional array: This is how far I got in the category_model: Code: function get_childnodes($id) { The result I would be returned in the controller as follow: Code: $result_array = $this->category_model->get_childnodes($id); I hope somebody can help me out with this one! Thanks in advance! Christophe Little help with recursive function plz - El Forum - 06-02-2011 [eluser]marjune[/eluser] hi! you have to make two arrays in it. Code: $childnodes = array(); //in side in this array would be another array for the name and id. Little help with recursive function plz - El Forum - 06-02-2011 [eluser]Jaketoolson[/eluser] Quote: $id_and_name[$idcounter] = $item->id; this would create the following array: Code: array( Just do this Code: $i = 0; Little help with recursive function plz - El Forum - 06-03-2011 [eluser]marjune[/eluser] $id_and_name[$idcounter] = $item->id; $id_and_name[$idcounter+1] = $item->name ; nope it doesn't return to because i often doing this stuff Code: array( Code: return $childnodes[$counter] = $id_and_name; @Jaketoolson: but the best is yours! ![]() Little help with recursive function plz - El Forum - 06-03-2011 [eluser]Christophe28[/eluser] Hi, I'm familiar on how to create a multidimensional array in a foreach, but how to create a multidimensional array in a recursive function? A recursive function is a function which basically calls itself, so you have to pass the array somehow to create one big multidim array with all the values and return it on the end ... Hope somebody can take a look. Christophe Little help with recursive function plz - El Forum - 06-10-2011 [eluser]marjune[/eluser] what i think about is, u need another function to call that initiate the multi arrays inside your recursion!! Little help with recursive function plz - El Forum - 06-10-2011 [eluser]Christophe28[/eluser] Hi, The problem is already solved. Thanks for all the input! I'll post a link when the article (about this topic) is done! Christophe Little help with recursive function plz - El Forum - 06-11-2011 [eluser]InsiteFX[/eluser] Your problem was because you were defining the array in your function, so everytime you re-entered it it resets the array! InsiteFX Little help with recursive function plz - El Forum - 06-11-2011 [eluser]Christophe28[/eluser] Exactly, that was part of the problem. I was also using the wrong function (array_push, instead of array_merge) ![]() |