1

I am trying to generate tree in php. here is my code for it

$company = array();
        $i = 1;
        foreach($final_array3 as $key => $value) {
            array_push($company, $value);
            $i++;
        }

Main array is

Array
(
    [Ordinary Income/Expense] => Array
        (
            [0] => stdClass Object
                (
                    [compid] => 1
                    [coaid] => 12877
                    [item] => Ordinary Income/Expense
                    [category] => 0
                    [Auto] => 0
                    [Jan_2015_Budget] => 0
                    [Feb_2015_Budget] => 0
                    [Mar_2015_Budget] => 0
                    [Apr_2015_Budget] => 0
                    [SUM(IF(`trl`.`entrydate` between '2016-1-01' and '2016-02-26', `trl`.`itemvalue`, 0))] => 0
                    [isTotal] => 0
                    [isparent] => 1
                )

            [1] => stdClass Object
                (
                    [compid] => 2
                    [coaid] => 13571
                    [item] => Ordinary Income/Expense
                    [category] => 0
                    [Auto] => 0
                    [Jan_2015_Budget] => 0
                    [Feb_2015_Budget] => 0
                    [Mar_2015_Budget] => 0
                    [Apr_2015_Budget] => 0
                    [SUM(IF(`trl`.`entrydate` between '2016-1-01' and '2016-02-26', `trl`.`itemvalue`, 0))] => 0
                    [isTotal] => 0
                    [isparent] => 0
                )

        )

    [Income] => Array
        (
            [0] => stdClass Object
                (
                    [compid] => 1
                    [coaid] => 12878
                    [item] => Income
                    [category] => 12877
                    [Auto] => 1
                    [Jan_2015_Budget] => 0
                    [Feb_2015_Budget] => 0
                    [Mar_2015_Budget] => 0
                    [Apr_2015_Budget] => 0
                    [SUM(IF(`trl`.`entrydate` between '2016-1-01' and '2016-02-26', `trl`.`itemvalue`, 0))] => 0
                    [isTotal] => 0
                    [isparent] => 1
                )

            [1] => stdClass Object
                (
                    [compid] => 2
                    [coaid] => 13572
                    [item] => Income
                    [category] => 13571
                    [Auto] => 1
                    [Jan_2015_Budget] => 0
                    [Feb_2015_Budget] => 0
                    [Mar_2015_Budget] => 0
                    [Apr_2015_Budget] => 0
                    [SUM(IF(`trl`.`entrydate` between '2016-1-01' and '2016-02-26', `trl`.`itemvalue`, 0))] => 0
                    [isTotal] => 0
                    [isparent] => 0
                )

        )
)

and desired output is

Array
(
    [Ordinary Income/Expense] => Array
        (
            [0] => stdClass Object
                (
                    [compid] => 1
                    [coaid] => 12877
                    [item] => Ordinary Income/Expense
                    [category] => 0
                    [Auto] => 0
                    [Jan_2015_Budget] => 0
                    [Feb_2015_Budget] => 0
                    [Mar_2015_Budget] => 0
                    [Apr_2015_Budget] => 0
                    [SUM(IF(`trl`.`entrydate` between '2016-1-01' and '2016-02-26', `trl`.`itemvalue`, 0))] => 0
                    [isTotal] => 0
                    [isparent] => 1
                    [children] => stdClass Object(
                                    [compid] => 1
                                    [coaid] => 12878
                                    [item] => Income
                                    [category] => 12877
                                    [Auto] => 1
                                    [Jan_2015_Budget] => 0
                                    [Feb_2015_Budget] => 0
                                    [Mar_2015_Budget] => 0
                                    [Apr_2015_Budget] => 0
                                    [SUM(IF(`trl`.`entrydate` between '2016-1-01' and '2016-02-26', `trl`.`itemvalue`, 0))] => 0
                                    [isTotal] => 0
                                    [isparent] => 1
                                )
                )

            [1] => stdClass Object
                (
                    [compid] => 2
                    [coaid] => 13571
                    [item] => Ordinary Income/Expense
                    [category] => 0
                    [Auto] => 0
                    [Jan_2015_Budget] => 0
                    [Feb_2015_Budget] => 0
                    [Mar_2015_Budget] => 0
                    [Apr_2015_Budget] => 0
                    [SUM(IF(`trl`.`entrydate` between '2016-1-01' and '2016-02-26', `trl`.`itemvalue`, 0))] => 0
                    [isTotal] => 0
                    [isparent] => 0
                    [children] => stdClass Object(
                            [compid] => 2
                            [coaid] => 13572
                            [item] => Income
                            [category] => 13571
                            [Auto] => 1
                            [Jan_2015_Budget] => 0
                            [Feb_2015_Budget] => 0
                            [Mar_2015_Budget] => 0
                            [Apr_2015_Budget] => 0
                            [SUM(IF(`trl`.`entrydate` between '2016-1-01' and '2016-02-26', `trl`.`itemvalue`, 0))] => 0
                            [isTotal] => 0
                            [isparent] => 0
                        )
                )
        )
)

So i want to make an category tree. parent child relation is coaid in stdclass object is parent and category with same as coaid key in stdcalss object is children.

now can you please suggest me how to do this.tree can be 5 level hierarchy.

Thanks

Sanjay Rathod
  • 1,083
  • 5
  • 18
  • 45
  • can you give us desired output? – mitkosoft Feb 26 '16 at 11:48
  • I suggest you this - http://stackoverflow.com/a/8587437/1278748 – Sardor Dushamov Feb 26 '16 at 11:50
  • simply its five level category treelike wordpress – Sanjay Rathod Feb 26 '16 at 11:50
  • An example with at least 10 elements, and the expected output would help to understand what your question is. As it is now, the relationship on *coaid* is ambiguous. For a 5-level nesting, all would need to have the same *coaid* value, and so it would be undefined at which level in the hierarchy each of them should appear. Please provide a good example. – trincot Feb 26 '16 at 12:01

0 Answers0