2011-11-30 35 views
2

所以,我得到这个阵列(将数据从数据库收集):Recursivly ordenate阵列ID和PARENT_ID结构

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [parent_id] => 0 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [parent_id] => 0 
     ) 

    [2] => Array 
     (
      [id] => 3 
      [parent_id] => 2 
     ) 

    [3] => Array 
     (
      [id] => 4 
      [parent_id] => 2 
     ) 

    [4] => Array 
     (
      [id] => 5 
      [parent_id] => 4 
     ) 
) 

,我试图创造和有序排列是这样的:

Array 
(
    [1] => Array 
     (
      [parent_id] => 0 
     ) 

    [2] => Array 
     (
      [parent_id] => 0 
      [children] => Array 
      (
       [3] => Array 
        (
         [parent_id] => 2 
        ) 

       [4] => Array 
        (
         [parent_id] => 2 
         [children] => Array 
         (
          [5] => Array 
           (
            [parent_id] => 4 
           ) 
         ) 
        ) 
      ) 
     ) 
) 

,我尝试用下面的代码:

function placeInParent(&$newList, $item) 
{ 
    if (isset($newList[$item['parent_id']])) 
    { 
     $newList[$item['parent_id']]['children'][$item['id']] = $item; 
     return true; 
    } 

    foreach ($newList as $newItem) 
    { 
     if (isset($newItem['children'])) 
     { 
      if (placeInParent($newItem['children'], $item)) 
      { 
       return true; 
      } 
     } 
    } 
    return false; 
} 

$oldList = (first array above) 

$newList = array(); 

foreach ($oldList as $item) 
{ 
    if ($item['parent_id'] == 0) 
    { 
     $newList[$item['id']] = $item; 
    } 
    else 
    { 
     placeInParent($newList, $item); 
    } 
} 

但问题是,我只得到第一个2级的ARR的唉!最后一个是失败..和我的有序阵列结果是这样的:

Array 
(
    [1] => Array 
     (
      [parent_id] => 0 
     ) 

    [2] => Array 
     (
      [parent_id] => 0 
      [children] => Array 
       (
        [3] => Array 
         (
          [parent_id] => 2 
         ) 

        [4] => Array 
         (
          [parent_id] => 2 
         ) 
       ) 
     ) 
) 

我只是无法得到我搞乱:\ help?

回答

5

您可以使用引用树内的节点索引的帮助下做到这一点没有递归:

$arr = array(
    array('id'=>1, 'parent_id'=>0), 
    array('id'=>2, 'parent_id'=>0), 
    array('id'=>3, 'parent_id'=>2), 
    array('id'=>4, 'parent_id'=>2), 
    array('id'=>5, 'parent_id'=>4), 
); 

// array to build the final hierarchy 
$tree = array(
    'children' => array(), 
    'path'  => array() 
); 

// index array that references the inserted nodes 
$index = array(0=>&$tree); 

foreach ($arr as $key => $val) { 
    // pick the parent node inside the tree by using the index 
    $parent = &$index[$val['parent_id']]; 
    // append node to be inserted to the children array 
    $node = array(
     'parent_id' => $val['parent_id'], 
     'path'  => $parent['path'] + array($val['id']) 
    ); 
    $parent['children'][$val['id']] = $node; 
    // insert/update reference to recently inserted node inside the tree 
    $index[$val['id']] = &$parent['children'][$val['id']]; 
} 

你正在寻找最终的阵列是$tree['children']

+0

那么......它的工作完美!我从来没有想过这会是“这么简单”!我想我会“过度思考”,并努力去实现它(而且应该是错误的)。 唯一的问题是,我不完全理解代码:\请问,请解释一下吗? – MGP

+0

@MARCOGPINTO你不了解什么? – Gumbo

+0

我有点理解逻辑,但让我们想象一下,我想添加一个字段,将路径保存到该节点,例如,对于节点5,我保存2-4-5。我没有准确地知道我在哪里可以做到这一点..:\ – MGP