使用 PHP 将 2d 数组转换为 3d

发布于 2024-09-25 08:14:18 字数 843 浏览 0 评论 0原文

有简单的二维数组和某种树,如下所示:

  • node1
    • 节点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',
                              ),
                 ),

    )
 ...
)

There are simple 2d array with some sort of tree like this:

  • node1
    • node2
      • node3

It's structure is:

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
    ),
)

Is there solutions with PHP to convert this array into:

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

    )
 ...
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡大本事 2024-10-02 08:14:18

找到@SO PHP 遍历函数将单个数组转换为带有子级的嵌套数组 - 基于父级 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);

Found @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);
浮光之海 2024-10-02 08:14:18
<?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'] = &$p[$id];

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

<?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);
?>

If each node can only have one child, then replace the one line with $p[$pid]['child'] = &$p[$id];.

(Edit: fixed it to work regardless of how the nodes are sorted.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文