父级递归地对PHP对象进行分组

时间:2013-05-21 15:13:59

标签: php arrays recursion grouping

我有一系列“门票”对象:

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

然后,我按照milestone_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

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

0 个答案:

没有答案