arrays - grouping PHP objects by parents recursively -


i have array of "tickets" objects :

array (size=31)   0 =>      object(ticketplus)       public 'values' =>          array (size=46)           'id' => int 62483,           'milestone_id' => int 75096,           'parent_ticket_id' => int 0           ...   1 =>     object(ticketplus)       public 'values' =>          array (size=46)           'id' => int 76603,           'milestone_id' => int 75096,           'parent_ticket_id' => int 62483           ...   2 =>     object(ticketplus)       public 'values' =>            array (size=46)             'id' => int 75402,             'milestone_id' => int 75096,             'parent_ticket_id' => int 62483             ...   3 =>     object(ticketplus)       public 'values' =>          array (size=46)             'id' => int 75403,             'milestone_id' => int 75096,             'parent_ticket_id' => int 75402             ...   4 =>     object(ticketplus)       public 'values' =>          array (size=46)             'id' => int 75098,             'milestone_id' => int 75097,             'parent_ticket_id' => int 0             ...   5 =>     object(ticketplus)       public 'values' =>          array (size=46)             'id' => int 76213,             'milestone_id' => int 75097,             'parent_ticket_id' => int 75098             ... 

and then, group them milestone_id, , end array :

array (size=3)   75096 =>      array (size=2)       'milestone' =>          object(milestone)[583]           public 'values' =>              array (size=46)               'id' => int 75096,       'objects' =>          array (size=4)           76602 =>              object(ticketplus)               ...           76603 =>              object(ticketplus)               ...           75402 =>              object(ticketplus)               ...           75403 =>              object(ticketplus)   75097 =>      array (size=2)       'milestone' =>          object(milestone)[587]             public 'values' =>          array (size=46)           'id' => int 75097,       'objects' =>          array (size=2)           75098 =>              object(ticketplus)[530]               ...           76213 =>              object(ticketplus)[569]               ... 

but can see in first array, there parent-child relation between these tickets, due "parent_ticket_id" field. , can imagine, i'd end array grouped milestones , parents. :

array (size=3)   75096 =>      array (size=2)       'milestone' =>          object(milestone)[583]           public 'values' =>              array (size=46)               'id' => int 75096,       'objects' =>          array (size=4)           76602 =>              object(ticketplus)               public 'children' =>                 array (size=2)                   76603 =>                       object(ticketplus)                        ...                   75402 =>                       object(ticketplus)                        public 'children' =>                          array(size=1)                            75403 =>                               object(ticketplus)   75097 =>      array (size=2)       'milestone' =>          object(milestone)[587]             public 'values' =>          array (size=46)           'id' => int 75097,       'objects' =>          array (size=2)           75098 =>              object(ticketplus)[530]               public 'children' =>                 array(size=1)                   76213 =>                      object(ticketplus)[569]                       ... 

and of course, there potentially infinite level of children, that's why guess should use recursive function group tickets parents.

my function transform raw array "grouped milestone" array following :

function groupbymilestone($objects, $min_state = state_visible, $min_visibility = visibility_normal) {   $result = array();    if(is_foreachable($objects)) {     $milestone_ids = objects_array_extract($objects, 'getmilestoneid');     if(is_foreachable($milestone_ids)) {       $milestones = milestones::findbyids($milestone_ids, $min_state, $min_visibility);       if(is_foreachable($milestones)) {         foreach($milestones $milestone) {           $result[$milestone->getid()] = array(             'milestone' => $milestone,             'objects' => array(),           );         } // foreach       } // if     } // if      // unknown milestone objects     $result[0] = array(       'milestone' => null,       'objects' => array(),     );       foreach($objects $key_obj => $object) {              if(isset($result[$object->getmilestoneid()])) {                 $result[$object->getmilestoneid()]['objects'][$object->getid()] = $object;             } else {                 $result[0]['objects'][$object->getid()] = $object;             } // if       } // foreach   } // if    return $result; } // groupbymilestone 

and have no idea how implement recursive parent grouping part. appreciated.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -