Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从列表构建递归数组_Php_Arrays_Recursion_Build_Multidimensional Array - Fatal编程技术网

PHP从列表构建递归数组

PHP从列表构建递归数组,php,arrays,recursion,build,multidimensional-array,Php,Arrays,Recursion,Build,Multidimensional Array,我从MySQL数据库返回一个页面及其父页面列表,并将所有结果放入一个数组,如下所示,其中每个结果都是一个数组,其中包括论坛的父、名称和id(数组页面的键也与页面id相同) 为了模型和应用,还有一些其他参数 “根页面”的父级为0 没有孤立页面 因此,MySQL查询将返回此数据集 pages=> [1] => array(id=>1, parent=>0, name=>Hello

我从MySQL数据库返回一个页面及其父页面列表,并将所有结果放入一个数组,如下所示,其中每个结果都是一个数组,其中包括论坛的父、名称和id(数组页面的键也与页面id相同)

为了模型和应用,还有一些其他参数

  • “根页面”的父级为0
  • 没有孤立页面
因此,MySQL查询将返回此数据集

pages=>
     [1] => array(id=>1, 
                  parent=>0, 
                  name=>Hello World)
     [2] => array(id=>1, 
                  parent=>1, 
                  name=>Child of Hello World)
     [3] => array(id=>1, 
                  parent=>0, 
                  name=>Brother of Hello World)
     [4] => array(id=>4, 
                  parent=>2, 
                  name=Grand-child of Hello World)
     [6] => array(id=>6, 
                  parent=>4, 
                  name=Great-grand-child of Hello World)
然后我想将数组转换成如下所示

pages=>
     [1] => id=>1, 
            name=>Hello World
            children=>

                [2] => id=>1
                       name=>Child of Hello World
                       children=>

                           [4] => 
                             id=>4
                             name=> Grand-child of Hello World)
                             children=>

                                 [6] => 
                                   id=>6
                                   name=> Great-grand-child of Hello World
                                   children= null

     [3] => array(id=>1, 
                  name=>Brother of Hello World
                  children=>null
所以基本上,我想把一个线性数组变成一个嵌套的多维数组,这样我就可以打印我的站点地图了

它需要是一个递归的解决方案。有700多页,最多5或6个级别


我只想做一个mysql查询。不是700,所以请不要给我一个基于mysql的解决方案。

这里有一个快速递归函数,用于构建树。请注意,它不是很好(一个原因是,它不会删除已经添加到树中的项目,因此每次递归时它都会遍历整个列表),但它应该足够有效,可以让您开始

<?php

$pages = array();
$pages[1] = array('id' => 1, 'parent' => 0, 'name' => 'Hello World');
$pages[2] = array('id' => 1, 'parent' => 1, 'name' => 'Child of Hello World');
$pages[3] = array('id' => 1, 'parent' => 0, 'name' => 'Brother of Hello World');
$pages[4] = array('id' => 4, 'parent' => 2, 'name' => 'Grand-child of Hello World');
$pages[6] = array('id' => 6, 'parent' => 4, 'name' => 'Great-grand-child of Hello World');

$children = array();
foreach($pages as $key => $page){
    $parent = (int)$page['parent'];
    if(!isset($children[$parent]))
        $children[$parent] = array();
    $children[$parent][$key] = array('id' => $page['id'], 'name' => $page['name']);
}

$new_pages = recursive_append_children($children[0], $children);

function recursive_append_children($arr, $children){
    foreach($arr as $key => $page)
        if(isset($children[$key]))
            $arr[$key]['children'] = recursive_append_children($children[$key], $children);
    return $arr;
}

print_r($new_pages);

?>
function buildTree($itemList, $parentId) {
  // return an array of items with parent = $parentId
  $result = array();
  foreach ($itemList as $item) {
    if ($item['parent'] == $parentId) {
      $newItem = $item;
      $newItem['children'] = buildTree($itemList, $newItem['id']);
      $result[] = $newItem;
    }
  }

  if (count($result) > 0) return $result;
  return null;
}

$myTree = buildTree($myArray, 0);

请使用您最喜欢的搜索引擎搜索“mysql嵌套集”。这是O(N)时间复杂性,因为两个循环都可以显示为只查看数组中的每个元素一次。感谢您的回复。只是做了一些小的调整,以适应整个数据阵列,但伟大的答案,谢谢。所以$children[$parent][$key]=array('id'=>$page['id'],'name'=>$page['name']);现在是$children[$parent][$key]=$pages[$key]@不客气。唯一的区别是,这将在生成的数组中保留
父项
值,这看起来像是您想要删除的。您可以总是
unset($children[$parent][$key]['parent'])虽然也一样。@Paulpro我知道这很旧,但我认为在第一个
foreach
中,应该是
$children[$parent][$key]
而不是
$children[$parent][$page['id']]
谢谢您的帮助。我没有用这个,但给了我一个好主意,我将如何做下一次。
function buildTree($itemList, $parentId) {
  // return an array of items with parent = $parentId
  $result = array();
  foreach ($itemList as $item) {
    if ($item['parent'] == $parentId) {
      $newItem = $item;
      $newItem['children'] = buildTree($itemList, $newItem['id']);
      $result[] = $newItem;
    }
  }

  if (count($result) > 0) return $result;
  return null;
}

$myTree = buildTree($myArray, 0);