2010-07-16 153 views
7

我有一个包含树数据的数组(通过父ID)。我想将其转换为多维数组。什么是实现这一目标的最佳方式?有没有什么短的功能?将平面阵列转换为多维

源阵列:

$source = array(
    '0' => array(
      'Menu' => array(
        'id' => 45 
        'name' => 'Home' 
        'parent_id' => 1 
      ) 
    ) 
    '1' => array(
      'Menu' => array(
        'id' => 47 
        'name' => 'Get started' 
        'parent_id' => 1 
      ) 
    ) 
    '2' => array(
      'Menu' => array(
        'id' => 72 
        'name' => 'Attributes' 
        'parent_id' => 71 
      ) 
    ) 
    '3' => array(
      'Menu' => array(
        'id' => 73 
        'name' => 'Headings' 
        'parent_id' => 71 
      ) 
    ) 
    '4' => array(
      'Menu' => array(
        'id' => 75 
        'name' => 'Links' 
        'parent_id' => 71 
      ) 
    ) 
    '5' => array(
      'Menu' => array(
        'id' => 59 
        'name' => 'Images' 
        'parent_id' => 75 
      ) 
    ) 
    '6' => array(
      'Menu' => array(
        'id' => 65 
        'name' => 'Lists' 
        'parent_id' => 75 
      ) 
    ) 
); 

有些家长从源阵列丢失。我希望缺少父项的项目是根。结果数组:

$result = array(
    '0' => array(
      'Menu' => array(
        'id' => 45 
        'name' => 'Home' 
        'parent_id' => 1 
      ) 
      'Children' => array() 
    ) 
    '1' => array(
      'Menu' => array(
        'id' => 47 
        'name' => 'Get started' 
        'parent_id' => 1 
      ) 
      'Children' => array() 
    ) 
    '2' => array(
      'Menu' => array(
        'id' => 72 
        'name' => 'Attributes' 
        'parent_id' => 71 
      ) 
      'Children' => array() 
    ) 
    '3' => array(
      'Menu' => array(
        'id' => 73 
        'name' => 'Headings' 
        'parent_id' => 71 
      ) 
      'Children' => array() 
    ) 
    '4' => array(
      'Menu' => array(
        'id' => 75 
        'name' => 'Links' 
        'parent_id' => 71 
      ) 
      'Children' => array(
        '0' => array(
         'Menu' => array(
          'id' => 59 
          'name' => 'Images' 
          'parent_id' => 75 
         ) 
         'Children' => array() 
        ) 
        '1' => array(
         'Menu' => array(
          'id' => 65 
          'name' => 'Lists' 
          'parent_id' => 75 
         ) 
         'Children' => array() 
        ) 
      ) 
    ) 
); 

更新:删除方括号。

+1

第一个已经是一个多维数组。多维仅仅意味着数组内部的数组。 – animuson 2010-07-16 01:09:29

+0

你在用蛋糕吗? – Young 2010-07-16 01:12:55

+0

这是不是有效的PHP ..什么[0] =>数组(...或['菜单'] =>数组(...意味着什么?所以我宁愿不理解你的输入形状数据 – 2010-07-16 01:14:15

回答

16

我不认为在PHP中有这样的内置函数。

我尝试下面的代码,它似乎工作准备嵌套数组你所描述的方法:

$nodes = array(); 
$tree = array(); 
foreach ($source as &$node) { 
    $node["Children"] = array(); 
    $id = $node["Menu"]["id"]; 
    $parent_id = $node["Menu"]["parent_id"]; 
    $nodes[$id] =& $node; 
    if (array_key_exists($parent_id, $nodes)) { 
    $nodes[$parent_id]["Children"][] =& $node; 
    } else { 
    $tree[] =& $node; 
    } 
} 

var_dump($tree); 

我写了一个PHP类我写我的介绍Hierarchical Models in SQL and PHP类似的算法,但我使用的是对象而不是普通数组。

+0

你太棒了!感谢工作解决方案和sush快速回答!源数组是数据库查询的结果。 – bancer 2010-07-16 01:55:28

+1

请注意,此算法仅适用于父母出现在其子项出现之前出现在数据库结果集中的情况。 – 2010-07-16 02:05:14

+0

不错的code.Take +1 – Oyeme 2012-02-15 12:38:15

0

我写了这个变种考虑根parent_id是0或缺失。无论父母在DB($源)还是不在孩子之后。

$source_by_id = array(); 
foreach ($source as &$row){ 
    $source_by_id[$row['id']] = &$row; 
} 
foreach ($source_by_id as $id => &$row){ 
    $source_by_id[ intval($row['parent_id']) ]['children'][$id] = &$row; 
} 
// remove cycling itself 
unset($source_by_id[0]['children'][0]); 

$result = $source_by_id[0]['children']; 

结果数组键是合适的ID。请享用!

0

我正在寻找如何使用类别来执行此操作的示例。这个例子假定父母总是有一个父母ID为'0'。这个例子是使用ZF2。

没有引用或递归。诀窍是在输出中,您查找[0]索引,并且为孩子指定parent_id作为索引。

$categoryLookup = $this->getCategoryLookup($associateById=true); 

if ($assignedCategories) {   
    $categoryHeirarchy = array(); 
    foreach($assignedCategories as $assignedCategory) { 
     $child = $categoryLookup[$assignedCategory->category_id]; 
     $parent = $categoryLookup[$child->parent_id];    
     $categoryHeirarchy[$child->parent_id][] = $categoryLookup[$child->category_id]; 
     $categoryHeirarchy[$parent->parent_id][$parent->category_id] = $categoryLookup[$parent->category_id]; 
    }   

    return $categoryHeirarchy; 
} 


<h3>Categories</h3> 
<dl class="dl-horizontal"> 
    <?php foreach($this->categoryHeirarchy[0] as $parent): ?> 
     <dt><?php echo $this->escapeHtml($parent->name); ?></dt> 
     <?php foreach($this->categoryHeirarchy[$parent->category_id] as $child): ?> 
      <dd><?php echo $this->escapeHtml($child->name); ?></dd> 
     <?php endforeach; ?> 
    <?php endforeach; ?>      
</dl>