如何在codeigniter中生成类别树?`

时间:2016-03-04 12:34:05

标签: php codeigniter

你好,我想生成类别树,如n级别的wordpress。

这是我的示例数组。

$arreay = Array
    (
        [0] => stdClass Object
            (
                [Id] => 12877
                [CoaName] => Ordinary Income/Expense
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 0
                [flgActive] => 1
            )

        [1] => stdClass Object
            (
                [Id] => 12878
                [CoaName] => Income
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 1
                [flgActive] => 1
            )

        [2] => stdClass Object
            (
                [CoaName] => I · ROOM REVENUE
                [ParentCat] => 1
                [Auto] => 23
                [flgActive] => 1
            )

        [3] => stdClass Object
            (
                [CoaName] => 400000 · ROOM REVENUE
                [ParentCat] => 23
                [Auto] => 24
                [flgActive] => 1
            )

        [4] => stdClass Object
            (
                [CoaName] => 410101 · Net Room Revenue Taxable
                [ParentCat] => 24
                [Auto] => 27
                [flgActive] => 1
            )

        [5] => stdClass Object
            (
                [CoaName] => 415101 · GTD No Show
                [ParentCat] => 24
                [Auto] => 32
                [flgActive] => 1
            )

        [6] => stdClass Object
            (
                [CoaName] => Total 400000 · ROOM REVENUE
                [ParentCat] => 23
                [Auto] => 35
                [flgActive] => 1
            )

        [7] => stdClass Object
            (
                [CoaName] => Total I · ROOM REVENUE
                [ParentCat] => 1
                [Auto] => 37
                [flgActive] => 1
            )

        [8] => stdClass Object
            (
                [CoaName] => II · OTHER OPERATING REVENUE
                [ParentCat] => 1
                [Auto] => 38
                [flgActive] => 1
            )

        [9] => stdClass Object
            (
                [CoaName] => 480000 · OTHER INCOME
                [ParentCat] => 38
                [Auto] => 39
                [flgActive] => 1
            )

        [10] => stdClass Object
            (
                [CoaName] => 480380 · Interest Income
                [ParentCat] => 39
                [Auto] => 49
                [flgActive] => 1
            )

        [11] => stdClass Object
            (
                [CoaName] => 480383 · Guest Laundry
                [ParentCat] => 39
                [Auto] => 51
                [flgActive] => 1
            )

        [12] => stdClass Object
            (
                [CoaName] => 480385 · Vending
                [ParentCat] => 39
                [Auto] => 53
                [flgActive] => 1
            )

        [13] => stdClass Object
            (
                [CoaName] => 480389 · Miscellaneous
                [ParentCat] => 39
                [Auto] => 54
                [flgActive] => 1
            )

        [14] => stdClass Object
            (
                [CoaName] => 480390 · Pet Fees
                [ParentCat] => 39
                [Auto] => 55
                [flgActive] => 1
            )

        [15] => stdClass Object
            (
                [CoaName] => 482000 · Sales Tax Discounts
                [ParentCat] => 39
                [Auto] => 70
                [flgActive] => 1
            )

        [16] => stdClass Object
            (
                [CoaName] => Total 480000 · OTHER INCOME
                [ParentCat] => 38
                [Auto] => 75
                [flgActive] => 1
            )

        [17] => stdClass Object
            (
                [CoaName] => Total II · OTHER OPERATING REVENUE
                [ParentCat] => 1
                [Auto] => 77
                [flgActive] => 1
            )

        [18] => stdClass Object
            (
                [Id] => 12955
                [CoaName] => Total Income
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 78
                [flgActive] => 1
            )

        [19] => stdClass Object
            (
                [Id] => 12956
                [CoaName] => Cost of Goods Sold
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 79
                [flgActive] => 1
            )

        [20] => stdClass Object
            (
                [CoaName] => COST OF OTHER OPERATING SALES
                [ParentCat] => 79
                [Auto] => 80
                [flgActive] => 1
            )

        [21] => stdClass Object
            (
                [Id] => 12968
                [CoaName] => Gross Profit
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 91
                [flgActive] => 1
            )

        [22] => stdClass Object
            (
                [Id] => 12969
                [CoaName] => Expense
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 92
                [flgActive] => 1
            )

        [23] => stdClass Object
            (
                [CoaName] => 10 · ROOMS DEPARTMENT
                [ParentCat] => 92
                [Auto] => 98
                [flgActive] => 1
            )

        [24] => stdClass Object
            (
                [CoaName] => 1015 · SALARY WAGES & BONUS
                [ParentCat] => 98
                [Auto] => 99
                [flgActive] => 1
            )

        [25] => stdClass Object
            (
                [CoaName] => 101501 · Front Desk
                [ParentCat] => 99
                [Auto] => 101
                [flgActive] => 1
            )

        [26] => stdClass Object
            (
                [CoaName] => 101503 · Housekeepers
                [ParentCat] => 99
                [Auto] => 103
                [flgActive] => 1
            )

        [27] => stdClass Object
            (
                [CoaName] => 101504 · Laundry
                [ParentCat] => 99
                [Auto] => 104
                [flgActive] => 1
            )

        [28] => stdClass Object
            (
                [CoaName] => 101505 · Hostess
                [ParentCat] => 99
                [Auto] => 105
                [flgActive] => 1
            )

        [29] => stdClass Object
            (
                [CoaName] => 101506 · Contract Labor
                [ParentCat] => 99
                [Auto] => 106
                [flgActive] => 1
            )

        [30] => stdClass Object
            (
                [CoaName] => 101507 · Bonuses & Incentives
                [ParentCat] => 99
                [Auto] => 107
                [flgActive] => 1
            )

        [31] => stdClass Object
            (
                [CoaName] => 101508 · Housekeeping Supervisor
                [ParentCat] => 99
                [Auto] => 108
                [flgActive] => 1
            )

        [32] => stdClass Object
            (
                [CoaName] => 6002 · SALARIES AND WAGES
                [ParentCat] => 99
                [Auto] => 109
                [flgActive] => 1
            )

        [33] => stdClass ObjectI had a 800 elements in a array now i am trying to generates its paren
            (
                [CoaName] => Total 1015 · SALARY WAGES & BONUS
                [ParentCat] => 98
                [Auto] => 116
                [flgActive] => 1
            )

        [34] => stdClass Object
            (
                [CoaName] => 1020 · PAYROLL RELATED EXPENSE
                [ParentCat] => 98
                [Auto] => 117
                [flgActive] => 1
            )

        [35] => stdClass Object
            (
                [CoaName] => 102001 · Employee Medical
                [ParentCat] => 117
                [Auto] => 118
                [flgActive] => 1
            )

        [36] => stdClass Object
            (
                [CoaName] => 102003 · Sick
                [ParentCat] => 117
                [Auto] => 120
                [flgActive] => 1
            )

        [37] => stdClass Object
            (
                [CoaName] => 102004 · Vacation
                [ParentCat] => 117
                [Auto] => 121
                [flgActive] => 1
            )

        [38] => stdClass Object
            (
                [CoaName] => 102005 · WORKERS COMP
                [ParentCat] => 117
                [Auto] => 122
                [flgActive] => 1
            )

        [39] => stdClass Object
            (
                [CoaName] => Total 1020 · PAYROLL RELATED EXPENSE
                [ParentCat] => 98
                [Auto] => 141
                [flgActive] => 1
            )

        [40] => stdClass Object
            (
                [CoaName] => 1030 · ROOM SUPPLIES
                [ParentCat] => 98
                [Auto] => 142
                [flgActive] => 1
            )

现在父类别和子类别关系介于ParentCat字段和自动字段之间。

现在我尝试了这段代码

foreach($tree as $el){
            $thisref = &$refs->{$el->Auto};
                foreach($el as $k => $v){
                @$thisref->{$k} = $v;
            }
            if($el->ParentCat == 0){
                $reportTree->{$el->Auto} = &$thisref;
            } 
            else{
                $refs->{$el->ParentCat}->children->{$el->Auto} = &$thisref;
            }
            $reportTree;
        }

我在数组中有800个元素,并尝试使用上面的代码生成树。 但单个元素在数组中插入100次。但它应该只插入一次。

父子关系 例如 在这里,我从数组中获取2个元素。

[1] => stdClass Object
            (
                [Id] => 12878
                [CoaName] => Income
                [CompId] => 1
                [ParentCat] => 0
                [Auto] => 1
                [flgActive] => 1
            )

        [2] => stdClass Object
            (
                [CoaName] => I · ROOM REVENUE
                [ParentCat] => 1
                [Auto] => 23
                [flgActive] => 1
            )

现在第二个元素的ParentCat值为1和 第一个元素具有Auto 1,因此第一个元素是第二个元素的父元素。 那么请你建议我怎么做。 谢谢

2 个答案:

答案 0 :(得分:1)

如果我理解正确 - 以下内容应该有效:

首先通过你的id键分配数组(根据你的php示例,它不总是一个id - 但我认为这是一个复制错误)

$arrTreeById = array();
$arrTree = array();

foreach($arrTree AS $objItem)
{
    $arrTreeById[$objItem->Id] = $objItem;
    $objItem->arrChilds = array();
}

在完成该作业后,您只需将孩子添加到父母

foreach($arrTreeById AS $objItem)
{
    if (isset($arrTree[$objItem->ParentCat]))   $arrTree[$objItem->ParentCat]->arrChilds[] = $objItem;
    if ($objItem->ParentCat == 0) $intStartingKey = $objItem->Id;
}

print_r($arrTree[$intStartingKey]);

或者我错过了什么?

答案 1 :(得分:0)

我已经为这个函数开发了一个函数来传递这个函数

function buildArrayTree($items){
        $ref = array(); // track references
        $Childrens = array(); // new array

        // menu reference since can't push directly later
        foreach($original = &$items as $item)
        {
            if($item->ParentCat == 0)
            {
                // add item
                $Childrens[$item->Auto] = array($item);

                // track item reference
                $ref[$item->Auto] = &$Childrens[$item->Auto];
            }
            else
            {
                // index of Childrens array
                $count = isset($ref[$item->ParentCat]['Childrens'])
                    ? count($ref[$item->ParentCat]['Childrens']) : 0;

                // parent already there? else move to back
                if(isset($ref[$item->ParentCat]))
                {
                    // set item under proper Childrens array item
                    $ref[$item->ParentCat]['Childrens'][] = array($item);

                    // track reference to new item
                    $ref[$item->Auto] = &$ref[$item->ParentCat]['Childrens'][$count];
                }
                else
                {
                    // if parent isn't there, add to end of array and we'll get to it later
                    array_push($original, $item);
                }
            }
        }
        return $Childrens;
    }