Php数组上的递归函数,用于树视图 [英] Php Recursive function on array for tree view

查看:106
本文介绍了Php数组上的递归函数,用于树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

  Array 

[1] =& b $ b [2] => 1
[3] => 2
[4] => 3
[5] => 0

此数组的键是唯一的,键。
like 1& 6是0,父的2是1,对于3是2 ....



我正在写一个递归函数,它将为给定的父ID找到一个树视图。
这里是我的代码:

  function recurviceChild($ parent,$ childParent,$ resultArr = array()){ 
foreach($ childParent as $ key => $ parentId){
if($ parent == $ parentId){
$ resultArr [$ parentId] [] = $ key;
$ resultArr = $ this-> recurviceChild($ key,$ childParent,$ resultArr);
}
}
return $ resultArr;
}

我创建的函数给我一级的深度结果。这个函数的结果如果我调用$ parent = 1($ childParent是上面给出的数组)是:

  Array 

[1] => Array

[0] => 2
[1] => 5


[2] => Array

[0] => 3


[3] =& b(
[0] => 4




b $ b

我希望这样的结果:

 数组

[1] => Array

[2] => Array

[3] => Array

[0] => ; 4




[2] => 5

pre>

或者帮助我创建树视图的东西。
提前谢谢。

解决方案

这是你想要的:

 <?php 

$ a = array

1 => 0,
=> 1,
3 => 2,
4 => 3,
5 => 1,
6 => 0
);

类节点{
var $ children;
public function __construct(){
$ this-> children = array();
}
}

$ tree = array();
foreach($ a as $ q => $ p){
if(!isset($ tree [$ p]))
$ tree [$ p] = new node;
if(!isset($ tree [$ q]))
$ tree [$ q] = new node;
$ mark [$ p] = FALSE;
$ mark [$ q] = FALSE;
array_push($ tree [$ p] - > children,$ q);
}

函数dfs(& $ ans,$ node){
全局$ tree,$ mark;
$ mark [$ node] = TRUE;
$ ans = array();
foreach($ tree [$ node] - > children as $ child)
if(!$ mark [$ child]){
$ ans [$ child] = $ child;
dfs($ ans [$ child],$ child);
}
}

$ parent = 1;

dfs($ ans,$ parent);

print_r($ ans);

?>


I have following array:

Array
(
    [1] => 0
    [2] => 1
    [3] => 2
    [4] => 3
    [5] => 1
    [6] => 0
)

keys of this array is unique, and values showing parent of key. like parent of 1 & 6 is 0, parent of 2 is 1, for 3 is 2....

I was writing a recursive function which will find a tree view for given parent id. here is my code:

function recurviceChild($parent, $childParent, $resultArr = array()) {
        foreach ($childParent as $key => $parentId) {
            if ($parent == $parentId) {
                $resultArr[$parentId][] = $key;
                $resultArr = $this->recurviceChild($key, $childParent, $resultArr);
            }
        }
        return $resultArr;
    }

The function I created give me result for depth of level one. result of this function if i call it for $parent=1 ($childParent is array given above) is:

Array
(
    [1] => Array
        (
            [0] => 2
            [1] => 5
        )

    [2] => Array
        (
            [0] => 3
        )

    [3] => Array
        (
            [0] => 4
        )

)

I m expecting result like this:

 Array
        (
            [1] => Array
                (
                    [2] => Array
                       (
                           [3] => Array
                                (
                                   [0] => 4
                                )
                       )

                )
             [2] => 5
        )

or something that help me to create a tree view. Thank you in advance.

解决方案

This does what you want:

<?php

$a= array
(
    1 => 0,
    2 => 1,
    3 => 2,
    4 => 3,
    5 => 1,
    6 => 0
);

class node {
    var $children;
    public function __construct(){
        $this->children = array();
    }
}

$tree = array();
foreach ($a as $q => $p){
    if(!isset($tree[$p]))
        $tree[$p] = new node;
    if(!isset($tree[$q]))
        $tree[$q] = new node;
    $mark[$p]=FALSE;
    $mark[$q]=FALSE;
    array_push($tree[$p]->children,$q);
}

function dfs(&$ans,$node){
    global $tree, $mark;
    $mark[$node] = TRUE;
    $ans = array();
    foreach($tree[$node]->children as $child)
        if(!$mark[$child]){
            $ans[$child]=$child;
            dfs($ans[$child],$child);
        }
}

$parent=1;

dfs($ans,$parent);

print_r($ans);

?>

这篇关于Php数组上的递归函数,用于树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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