emehrkay Posted March 10, 2007 Share Posted March 10, 2007 say i have, but with about 50 objects Array ( [0] => stdClass Object ( [navigation_id] => 1 [parent_nav_id] => 0 [display_name] => Home [link_id] => top:home [nav_link] => ./home.php [date_created] => 2007-03-09 16:51:26 [date_modified] => 2007-03-09 16:51:26 ) [1] => stdClass Object ( [navigation_id] => 2 [parent_nav_id] => 0 [display_name] => Music [link_id] => top:music [nav_link] => # [date_created] => 2007-03-09 16:51:26 [date_modified] => 2007-03-09 16:51:26 ) ) each have a parent_nav_id. what i want to do is loop through this array and create other arrays grouped by parent id. then from there i want to take all of those arrays and make it a multidim based on the parent/child relationship so id have this array(top => array(all of the arrays with 0 for parent), 'mid' => array(all of the arrays whose parent is one in the top), 'bot' => array(all of the arrays whose parent is in mid)) etc etc my problem is creating the new arrays and storing them so that i can later group them. any suggestions? thanks Quote Link to comment https://forums.phpfreaks.com/topic/42138-how-can-i-turn-an-arry-of-objects-into-a-multidimensional-array/ Share on other sites More sharing options...
tarun Posted March 10, 2007 Share Posted March 10, 2007 1 Question Whats A Multi Dimensional Array? Quote Link to comment https://forums.phpfreaks.com/topic/42138-how-can-i-turn-an-arry-of-objects-into-a-multidimensional-array/#findComment-204389 Share on other sites More sharing options...
per1os Posted March 10, 2007 Share Posted March 10, 2007 This is fun stuff, If you can create (write out your own) array definition that would be helpful than I can show you how to do it automatically. tarun, a multi-dimensional array is an array within an array, for example: $multDimArr = array("array1" => array("multiArr" => 1)); That is a multi-dimensional array. To access "multiArr" you would do this: $multDimArr["array1"]["multiArr"] which contains "1". --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42138-how-can-i-turn-an-arry-of-objects-into-a-multidimensional-array/#findComment-204391 Share on other sites More sharing options...
emehrkay Posted March 10, 2007 Author Share Posted March 10, 2007 frost, i already have the structure defined, i created it manually. What i am trying to do now is modify a result set from a query to fit that mold. my first post shows my result here is the final structure. after it is in this format, i create unordered lists based on relationships [that is already done] i thought that my array game was pretty solid, i guess i need to work on it a lot thanks for the help Array ( [top] => Array ( [0] => Array ( [id] => top:top [Home] => Array ( [id] => top:home [link] => home.php ) [Music] => Array ( [id] => top:music [link] => javascript:void(0); ) [Members] => Array ( [id] => top:mem [link] => javascript:void(0); ) [Discussions] => Array ( [id] => top:dis [link] => javascript:void(0); ) [Logout] => Array ( [id] => top:logout [link] => login.php?action=logout ) ) ) [mid] => Array ( [members] => Array ( [id] => mid:mem [users] => Array ( [id] => mid:usr [link] => javascript:void(0); ) [Top 8] => Array ( [id] => mid:top8 [link] => javascript:void(0); ) ) [music] => Array ( [id] => mid:music [Artists] => Array ( [id] => mid:artists [link] => javascript:void(0); ) [Genres] => Array ( [id] => mid:genres [link] => javascript:void(0); ) [Albums] => Array ( [id] => mid:albums [link] => javascript:void(0); ) ) [discussion] => Array ( [id] => mid:dis [search] => Array ( [id] => mid:searchDis [link] => javascript:void(0); ) [Disp Holder] => Array ( [id] => mid:holder [link] => javascript:void(0); ) ) ) [bot] => Array ( [members] => Array ( [id] => bot:usr [search] => Array ( [id] => bot:search [link] => javascript:void(0); ) [browse] => Array ( [id] => bot:browse [link] => javascript:void(0); ) [Message] => Array ( [id] => bot:message [link] => javascript:void(0); ) ) [top8] => Array ( [id] => bot:top8 [View] => Array ( [id] => bot:view [link] => javascript:void(0); ) ) [artists] => Array ( [id] => bot:artists [browse A-Z] => Array ( [id] => bot:artBrowse [link] => javascript:void(0); ) [search] => Array ( [id] => bot:artSearch [link] => # ) ) [genres] => Array ( [id] => bot:genres [Country] => Array ( [id] => bot:country [link] => javascript:void(0); ) [Rap] => Array ( [id] => bot:rap [link] => javascript:void(0); ) [R & B] => Array ( [id] => bot:rb [link] => javascript:void(0); ) ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/42138-how-can-i-turn-an-arry-of-objects-into-a-multidimensional-array/#findComment-204401 Share on other sites More sharing options...
per1os Posted March 10, 2007 Share Posted March 10, 2007 To be honest with you, I would love to see working code of this. I tried doing a few category recursion scripts with no luck. I can drilldown to the 3rd one but after that its all posh. If you do find the answer please post it here as I would be interested in seeing the code. --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42138-how-can-i-turn-an-arry-of-objects-into-a-multidimensional-array/#findComment-204407 Share on other sites More sharing options...
Barand Posted March 11, 2007 Share Posted March 11, 2007 I tried with arrays and sub array but my recursive function struggled with the reference to the subarrays and insisted on passing copies instead. So I took another approach and created a recursive "tree" class containing object reference and its children (Note: PHP5 as v4 doesn't handle object references by default.) <?php class myObj { public $id; public $parent; public $name; function __construct ($id, $parent, $name) { $this->id = $id; $this->parent = $parent; $this->name = $name; } } class myTree { public $obj; public $children; function __construct ($obj) { $this->obj = $obj; $this->children = array(); } function addChild ($obj) { $this->children[] = new myTree($obj); } function printTree ($level) { if ($this->obj) { $indent = str_repeat(' ', $level*5); echo $indent, $this->obj->id, ' : ', $this->obj->name, "<br>" ; } foreach ($this->children as $c) { $c->printTree($level+1); } } } /** * test array (id, parent, name) */ $objArray = array( 1 => new myObj (1, 0, 'a'), new myObj (2, 0, 'b'), new myObj (3, 0, 'c'), new myObj (4, 1, 'd'), new myObj (5, 2, 'e'), new myObj (6, 3, 'f'), new myObj (7, 1, 'g'), new myObj (8, 3, 'h'), new myObj (9, 2, 'j'), new myObj (10, 3, 'k'), new myObj (11, 1, 'l'), new myObj (12, 2, 'm'), new myObj (13, 3, 'n'), new myObj (14, 1, 'p'), new myObj (15, 3, 'q'), new myObj (16, 4, 'r'), new myObj (17, 4, 's'), new myObj (18, 5, 't'), new myObj (19, 16, 'u') ); $results = new myTree(null); foreach ($objArray as $obj) { /** * if parent is 0 add directly to root of tree */ if ($obj->parent == 0) { $results->addChild($obj); } /** * otherwise search each children array to if it contains the parent */ else { add_to_children ($obj, $results->children); } } function add_to_children ($obj, $ancestor) { foreach ($ancestor as $tree) { /** * if parent found, add child */ if ($tree->obj->id == $obj->parent) { $tree->addChild($obj); } /** * otherwise search its children */ else add_to_children($obj, $tree->children) ; } } $results->printTree(0); ?> Quote Link to comment https://forums.phpfreaks.com/topic/42138-how-can-i-turn-an-arry-of-objects-into-a-multidimensional-array/#findComment-204734 Share on other sites More sharing options...
per1os Posted March 11, 2007 Share Posted March 11, 2007 Hey barand, thanks for that code. I am going to try and apply it for my uses! Really appreciate it bud. --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42138-how-can-i-turn-an-arry-of-objects-into-a-multidimensional-array/#findComment-204797 Share on other sites More sharing options...
emehrkay Posted March 11, 2007 Author Share Posted March 11, 2007 barand im going to try that in a few. thanks for your help the only issue i see with your code is that i want to return actual arrays. maybe i can modify what you created to do so Quote Link to comment https://forums.phpfreaks.com/topic/42138-how-can-i-turn-an-arry-of-objects-into-a-multidimensional-array/#findComment-204938 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.