Search code examples
phparraysrecursionbyref

Flat array to multilevel structure - php array


Currently I have a table like so:

+-----------+---------------+-------------+-------------+
| category  | OrderID       | Name        | Name2       |
+-----------+---------------+-------------+-------------+
| ABC       | 12345         | Pen         | Black       |
+-----------+---------------+-------------+-------------+
| ABC       | 34545         | Pencil      | White       |   
+-----------+---------------+-------------+-------------+
| ABC       | 34545         | Pen         | Black       |
+-----------+---------------+-------------+-------------+
| DEF       | 12345         | Pencil      | Black       |
+-----------+---------------+-------------+-------------+
| DEF       | 12345         | Pen         | White       |
+-----------+---------------+-------------+-------------+

I like to have this in the tree format, I have read so many examples on ref. and recursive but didn't grasp the concepts. Can someone help?

Since the table will be huge - maybe around 20K records - not sure recursive would have any impact on the performance etc.

I have to have it done in this format due to the client html widget restrictions:

Array
(
[0] => Array
    (
        [category] => ABC
        [children] => Array
            (
            [0] => Array(
                [OrderID] => 12345
                [children] => Array
                    (
                        [Name] => Pen
                        [Name2] => Black

                    )
            )
            [1] => Array(
                [OrderID] => 34545
                [children] => Array
                (
                    [0] => Array(
                        [Name] => Pencil
                        [Name2] => White
                    )
                    [1] => Array(
                        [Name] => Pen
                        [Name2] => White
                    )
                )
            )

    )

[1] => Array
    (
        [category] => DEF
        [children] => Array
            (
                [OrderID] => 12345
                [children] => Array
                (
                    [0] => Array(
                        [Name] => Pencil
                        [Name2] => Black
                    )
                    [1] => Array(
                        [Name] => Pen
                        [Name2] => White
                    )
                )
            )
    )

Solution

  • Putting the value that need to be grouped into array content is not a good idea in many cases, because you can't use the value as lookup-key, thus making the grouping more difficult and inefficient.

    $ori=json_decode(<<<JSON
    [{"category":"ABC","orderID":"12345","Name":"Pen","Name2":"Black"},
    {"category":"ABC","orderID":"34545","Name":"Pencil","Name2":"White"},
    {"category":"ABC","orderID":"34545","Name":"Pen","Name2":"Black"},
    {"category":"DEF","orderID":"12345","Name":"Pencil","Name2":"Black"},
    {"category":"DEF","orderID":"12345","Name":"Pen","Name2":"White"}]
    JSON
    ,true);
    $result=array();
    while($row=array_shift($ori))//Pretend that we're fetching record from DB
    {
        $result[$row["category"]][$row["orderID"]][]=array("Name"=>$row["Name"],"Name2"=>$row["Name2"]);
    }
    print_r($result);
    

    By using the grouped value (category and orderID) as array key, you make the finding and grouping more efficient. The above code prints out:

    Array
    (
        [ABC] => Array
            (
                [12345] => Array
                    (
                        [0] => Array
                            (
                                [Name] => Pen
                                [Name2] => Black
                            )
    
                    )
    
                [34545] => Array
                    (
    ......
    

    You still get the same count() results, and gets the benefit of finding certain category and order more easily: $result[category][order]["Name"]=....

    But if you insist on getting your desired format, you can still add:

    array_walk($result,function(&$v,$k){
        $x=array("category"=>$k,"children"=>array());
        array_walk($v,function($arr,$oid)use(&$x){
            $x["children"][]=array("orderID"=>$oid,"children"=>$arr);
        });
        $v=$x;
    });
    print_r(array_values($result));
    

    This outputs:

    Array
    (
        [0] => Array
            (
                [category] => ABC
                [children] => Array
                    (
                        [0] => Array
                            (
                                [orderID] => 12345
                                [children] => Array
                                    (
                                        [0] => Array
                                            (
                                                [Name] => Pen
                                                [Name2] => Black
                                            )
    
                                    )
    
                            )
    
                        [1] => Array
                            (
                                [orderID] => 34545
    ......
    

    Demo. Anonymous function requires PHP>=5.3