-1

目前我有一张这样的桌子:

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

我喜欢树格式,我已经阅读了很多关于 ref 的示例。和递归,但没有掌握概念。有人可以帮忙吗?

由于表会很大 - 可能有大约 20K 条记录 - 不确定递归会对性能等产生任何影响。

由于客户端 html 小部件的限制,我必须以这种格式完成:

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
                    )
                )
            )
    )
4

1 回答 1

0

将需要分组的值放入数组内容中在很多情况下并不是一个好主意,因为不能将该值用作lookup-key,从而使分组更加困难和低效。

$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);

通过使用分组值 (categoryorderID) 作为数组,您可以更有效地查找和分组。上面的代码打印出来:

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

                )

            [34545] => Array
                (
......

您仍然会得到相同的count()结果,并且可以更轻松地找到特定的类别和订单:$result[category][order]["Name"]=....

但是如果你坚持得到你想要的格式,你仍然可以添加:

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

这输出:

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

                                )

                        )

                    [1] => Array
                        (
                            [orderID] => 34545
......

演示。匿名函数需要 PHP>=5.3

于 2013-07-23T08:13:11.470 回答