【问题标题】:PHP: Building a hierarchistic array structurePHP:构建分层数组结构
【发布时间】:2012-06-18 08:11:37
【问题描述】:

我在构建分层数组结构时遇到了一些问题 - 我几乎完成了,但是有一些无法解释的东西让我的数组看起来很奇怪,我希望你能帮助我。

树形结构为:

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:

Big picture

具有 2 个子项(或更多?- 没有测试值)的每个项目都按应有的方式获取第一项,但第二项也包含第一项 - 复制所有结果。

任何帮助表示赞赏。

【问题讨论】:

    标签: php json hierarchy


    【解决方案1】:

    使用array_push 代替array_merge - 这将添加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));
                    }
                } 
            }
    

    【讨论】:

    • 我在实现这个时遇到了一些问题:遇到了 PHP 错误 严重性:警告消息:array_push() 期望参数 1 是数组,给定整数
    • 添加 print_r($parentArray);和 print_r($childArray);都打印数组结构。
    • 那么错误怎么可能说$parentArray是整数?尝试对所有循环和递归使用var_dump($parrentArray);...当$parentArray 变为整数时,一定有一个点...
    【解决方案2】:

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

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      相关资源
      最近更新 更多