1

我有一组“票”对象:

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
            ...

然后,我将它们按里程碑 ID 分组,最后得到这个数组:

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]
              ...

但是正如您在第一个数组中看到的那样,由于它们的“parent_ticket_id”字段,这些票之间存在父子关系。正如你可以想象的那样,我想最终得到一个按里程碑和父母分组的数组。类似的东西:

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]
                      ...

当然,孩子的数量可能是无限的,这就是为什么我想我应该使用递归函数来按父母对我的票进行分组。

我将原始数组转换为“按里程碑分组”数组的函数如下:

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 as $milestone) {
          $result[$milestone->getId()] = array(
            'milestone' => $milestone,
            'objects' => array(),
          );
        } // foreach
      } // if
    } // if

    // For unknown milestone objects
    $result[0] = array(
      'milestone' => null,
      'objects' => array(),
    );


    foreach($objects as $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

而且我不知道如何实现递归父分组部分。任何帮助,将不胜感激。

4

0 回答 0