从列表中PHP构建递归阵列 [英] PHP Building Recursive Array from List

查看:150
本文介绍了从列表中PHP构建递归阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我返回的页面,并从MySQL数据库其父页的列表,并把所有结果到一个数组如下,其中每一个结果是一个数组,其中包括论坛的父,名称和ID(阵列页面的关键也是一样页面ID)。

有关的模型和应用程序了的缘故,也有一些其他参数。


  • 根页有0父

  • 没有孤立的页面

因此​​,MySQL的查询将返回该数据集。

 页=>
     [1] =>阵列(ID =大于1,
                  父=大于0,
                  名称=>的Hello World)
     [2] =>阵列(ID =大于1,
                  父=大于1,
                  NAME =>的Hello World的孩子)
     [3] =>阵列(ID =大于1,
                  父=大于0,
                  NAME =>的Hello World的兄弟)
     [4] =>阵列(ID =→4,
                  父=大于2,
                  名称=的Hello World的大孩子)
     [6] =>阵列(ID =→6,
                  父=→4,
                  命名的Hello World的大= - 盛大的孩子)

我会再像数组转换成的东西,看起来像这样

 页=>
     [1] => ID =大于1,
            名称=>的Hello World
            孩子= GT;                [2] => ID =大于1
                       NAME =>的Hello World的儿童
                       孩子= GT;                           [4] =>
                             ID =→4
                             名称= GT;你好世界的大孩子)
                             孩子= GT;                                 [6] =>
                                   ID =→6
                                   名称= GT;你好世界的伟大盛大孩子
                                   孩子= NULL     [3] =>阵列(ID =大于1,
                  NAME =>的Hello World的兄弟
                  孩子= GT;空

所以基本上,我想转线性阵列到嵌套多维数组,这样我可以打印我的站点地图。

它需要递归溶液。有超过700页和5个或6个等级。

我只希望做1 MySQL查询。没有700那么请不要给我一个MySQL为基础的解决方案。


解决方案

 < PHP$页=阵列();
$页[1] =阵列('ID'=大于1,父=大于0,'名'=>的'Hello World');
$页[2] =阵列('ID'=大于1,父=大于1,'名'=>的'Hello World的孩子');
$页[3] =阵列('ID'=大于1,父=大于0,'名'=>的'Hello World的哥');
$页[4] =阵列('ID'=> 4,父=大于2,'名'=>'大孩子的Hello World的');
$页[6] =阵列('ID'=> 6,'父'=> 4,'名'= GT;的'Hello World的大 - 大孩子');$儿童=阵列();
的foreach($页为$关键=> $页){
    $父=(INT)$页['父'];
    如果(!使用isset($儿[$父))
        $儿[$父=阵列();
    $儿[$父] [$关键] =阵列('ID'=> $页['身份证'],'名'=> $页['名']);
}$ new_pages = recursive_append_children($儿[0],$子女);功能recursive_append_children($改编,$子女){
    的foreach($改编为$关键=> $页)
        如果(使用isset($儿[$关键]))
            $改编[$关键] ['孩子'] = recursive_append_children($儿[$键],$子女);
    返回$ ARR;
}的print_r($ new_pages);?>

输出:

 阵列

    [1] =>排列
        (
            [ID] => 1
            [名] =>你好,世界
            [儿童] =>排列
                (
                    [2] =>排列
                        (
                            [ID] => 1
                            [名] =>你好世界儿童
                            [儿童] =>排列
                                (
                                    [4] =>排列
                                        (
                                            [ID] => 4
                                            [名] =>你好世界的大孩子
                                            [儿童] =>排列
                                                (
                                                    [6] =>排列
                                                        (
                                                            [ID] => 6
                                                            [名] =>你好世界的伟大盛大孩子
                                                        )                                                )                                        )                                )                        )                )        )    [3] =>排列
        (
            [ID] => 1
            [名] =>你好世界的兄弟
        )

I am returning a list of pages and their parent pages from a MySQL database and putting all results into an array as follows where every result is an array which includes the parent, name and id of the forum (the key of array pages is also the same as page id).

For the sake of the model and the applicaiton, there are some other parameters.

  • "root pages" have a parent of 0
  • there are no orphaned pages

so, the MySQL query will return this dataset.

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)

i would then like to transform the array into something that looks like this

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

So basically, i want to turn a linear array into a nested multidimensional array so that i can print my sitemap.

it needs to be a recursive solution. there are over 700 pages and up to 5 or 6 levels.

i only want to do 1 mysql query. not 700 so please dont give me a mysql based solution.

解决方案

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

?>

Outputs:

Array
(
    [1] => Array
        (
            [id] => 1
            [name] => Hello World
            [children] => Array
                (
                    [2] => Array
                        (
                            [id] => 1
                            [name] => Child of Hello World
                            [children] => Array
                                (
                                    [4] => Array
                                        (
                                            [id] => 4
                                            [name] => Grand-child of Hello World
                                            [children] => Array
                                                (
                                                    [6] => Array
                                                        (
                                                            [id] => 6
                                                            [name] => Great-grand-child of Hello World
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [3] => Array
        (
            [id] => 1
            [name] => Brother of Hello World
        )
)

这篇关于从列表中PHP构建递归阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆