PHP:构建分层数组结构

时间:2012-06-18 08:11:37

标签: php json hierarchy

我在构建分层数组结构时遇到了一些问题 - 我几乎完成了它,但是有一些原因不明,这使得我的数组看起来很奇怪,我希望你可以帮助我。

树结构是:

root
|data
|-children
|--data
|--children
|---data
|---children

任何孩子都可以有任意数量的孩子,每个孩子可以有任意数量的父母。

我有一个构建树的函数:

private function build_tree($rows,$parent) {
    $i = 0;
    $response -> result = array();
    $result = array();
    $leaf = false;

    foreach($rows as $row) {
        if($row['parent_id'] == $parent) {
            $leaf = is_null($row['treeDef']) ? false : true;
            $workingSet = array_merge($result,array(                
                'id' => (int)$i, 
                'parent_id' => (int)$row['parent_id'], 
                'child_id' => (int)$row['id'], 
                'name' => (string)$row['resourceName'], 
                'updated' => strtotime($row['updated']), 
                'purchasedUnit' => (string)$row['purchasingUnit'], 
                'purchasedCost' => (double)$row['purchasingCosts'], 
                'purchasedDiscount' => (double)$row['discount'], 
                'estimateUnit' => (string)$row['estimatingUnit'], 
                'profitAddOn' => (string)$row['profitAddOn'], 
                'landedCost' => (double)$row['landedCost'], 
                'unitCorrelation' => (double)$row['unitCorrelation'], 
                'leadTime' => (string)$row['leadTime'], 
                'ResourceClassShortname' => (string)$row['ResourceClassShortname'], 
                'supplierName' => (string)$row['nameSupplier'],
                'iconCls' => (string)$row['typeIcon'],
                'leaf' => $leaf
            ));
            $hasChildren = $this->Resource_model->has_children($rows,$row['id']);
            if ($hasChildren->check) {
                if (!$leaf) {
                    for($j=0; $j <= ($hasChildren -> rows); $j++) {
                        $parentArray = $workingSet;
                        $childArray = $this -> Resource_model -> build_tree($rows,$row['id']);
                        $workingSet = array_merge($parentArray,array('children' => $childArray -> result));
                    }
                } 
            }
            $result[$i] = $workingSet;
            $i++;
        }
    }

    $response -> result = $result;
    $response -> rows = $i; 
    return $response;
}

哪个产生这个JSON:

PrintScreen

Big picture

每个有2个孩子(或更多? - 没有测试值)的项目会得到第一个项目,但第二个项目也包含第一个项目 - 复制所有结果。

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:1)

而不是array_merge使用array_push - 这将添加children子阵列,而不是尝试将其与已存在的子阵列合并...

这部分代码(已编辑):

        $hasChildren = $this->Resource_model->has_children($rows,$row['id']);
        if ($hasChildren->check) {
            if (!$leaf) {
                for($j=0; $j <= ($hasChildren->rows); $j++) {
                    $parentArray = $workingSet;
                    $childArray = $this->Resource_model->build_tree($rows,$row['id']);
                    $workingSet = array_push($parentArray,array('children' => $childArray->result));
                }
            } 
        }

答案 1 :(得分:0)

让它工作,这是最终的代码:

private function build_tree($rows,$parent) {
    $i = 0;
    $response -> result = array();
    $result = array();
    $leaf = false;
    $newArray = array();

    foreach($rows as $row) {
        if($row['parent_id'] == $parent) {
            $leaf = is_null($row['treeDef']) ? false : true;
            $newArray = array(              
                'id' => (int)$i, 
                'parent_id' => (int)$row['parent_id'], 
                'child_id' => (int)$row['id'], 
                'name' => (string)$row['resourceName'], 
                'updated' => strtotime($row['updated']), 
                'purchasedUnit' => (string)$row['purchasingUnit'], 
                'purchasedCost' => (double)$row['purchasingCosts'], 
                'purchasedDiscount' => (double)$row['discount'], 
                'estimateUnit' => (string)$row['estimatingUnit'], 
                'profitAddOn' => (string)$row['profitAddOn'], 
                'landedCost' => (double)$row['landedCost'], 
                'unitCorrelation' => (double)$row['unitCorrelation'], 
                'leadTime' => (string)$row['leadTime'], 
                'ResourceClassShortname' => (string)$row['ResourceClassShortname'], 
                'supplierName' => (string)$row['nameSupplier'],
                'iconCls' => (string)$row['typeIcon'],
                'leaf' => $leaf
            );

            $hasChildren = $this -> Resource_model -> has_children($rows,$row['id']);
            if ($hasChildren->check) {
                for($j=0; $j <= ($hasChildren -> rows); $j++) {
                    $childArray = $this -> Resource_model -> build_tree($rows,$row['id']);
                    $newArray = array_merge($newArray, array('children' => $childArray -> result));
                }
            }
            $result[$i] = $newArray;
            $i++;
        }
    }

    $response -> result = $result;
    $response -> rows = $i; 
    return $response;
}