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