【问题标题】:Convert 2d array into 3d with PHP使用 PHP 将 2d 数组转换为 3d
【发布时间】:2010-09-30 02:45:49
【问题描述】:

有一些像这样的树的简单二维数组:

  • 节点1
    • 节点2
      • 节点3

它的结构是:

array(
    array (
      'id' : 1,
      'pid': 0,
      'title' : 'node1',
      'level' : 1
    ),
    array (
      'id' : 2,
      'pid': 1,
      'title' : 'node2',
      'level' : 2
    ),
    array (
      'id' : 3,
      'pid': 2,
      'title' : 'node3',
      'level' : 3
    ),
)

是否有 PHP 解决方案可以将此数组转换为:

array(
    array (
      'id' : 1,
      'title' : 'node1',
      'child' :  array (
                   'id' : 2,
                   'title' : 'node2',
                   'child' :  array (
                                 'id' : 3,
                                 'title' : 'node3',
                              ),
                 ),

    )
 ...
)

【问题讨论】:

  • 数组并不是一个理想的结构。你打算用它做什么?
  • 嗨 :) 这听起来像是一个非常具体的问题,我怀疑有一个简单的方法可以调用。
  • 您是否总是有想要转换的二维输入?是否有任何转换规则,或者它们都像您的示例中那样“顺序”?
  • 好吧,我的手机一直以不同的 cmets 提交不同的行,对不起
  • :) 我有一些“目录”结构。很深的一个。而且我知道我可以使用我请求的带有节点数组的递归模板输出它。并在 SO 找到了解决方案。 stackoverflow.com/questions/2094207/…

标签: php arrays tree


【解决方案1】:

找到@SO PHP Traversing Function to turn single array into nested array with children - based on parent id

$inArray = array(
    array('ID' => '1', 'parentcat_ID' => '0'),
    array('ID' => '2', 'parentcat_ID' => '0'),
    array('ID' => '6', 'parentcat_ID' => '1'),  
    array('ID' => '7', 'parentcat_ID' => '1'),
    array('ID' => '8', 'parentcat_ID' => '6'),          
    array('ID' => '9', 'parentcat_ID' => '1'),  
    array('ID' => '13', 'parentcat_ID' => '7'),
    array('ID' => '14', 'parentcat_ID' => '8'),     
);

function makeParentChildRelations(&$inArray, &$outArray, $currentParentId = 0) {
    if(!is_array($inArray)) {
        return;
    }

    if(!is_array($outArray)) {
        return;
    }

    foreach($inArray as $key => $tuple) {
        if($tuple['parentcat_ID'] == $currentParentId) {
            $tuple['children'] = array();
            makeParentChildRelations($inArray, $tuple['children'], $tuple['ID']);
            $outArray[] = $tuple;   
        }
    }
}

$outArray = array();
makeParentChildRelations($inArray, $outArray);

print_r($outArray);

【讨论】:

    【解决方案2】:
    <?php
    $p = array(0 => array());
    foreach($nodes as $n)
    {
      $pid = $n['pid'];
      $id = $n['id'];
    
      if (!isset($p[$pid]))
        $p[$pid] = array('child' => array());
    
      if (isset($p[$id]))
        $child = &$p[$id]['child'];
      else
        $child = array();
    
      $p[$id] = $n;
      $p[$id]['child'] = &$child;
      unset($p[$id]['pid']);
      unset($p[$id]['level']);
      unset($child);
    
      $p[$pid]['child'][] = &$p[$id];    
      // $p[$pid]['child'] = &$p[$id]; // or this, if only one child
    }
    $nodes = $p['0']['child'];
    unset($p);
    ?>
    

    如果每个节点只能有一个子节点,则将这一行替换为$p[$pid]['child'] = &amp;$p[$id];

    (编辑:无论节点如何排序,它都可以正常工作。)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 2021-12-29
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    相关资源
    最近更新 更多