PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php $data = [ 75 => [ "groupName" => "Японская кухня", "groupId" => 100001, "hasChilds" => 1, ], 76 => [ "groupName" => 'Суши', "groupId" => 101705, "hasChilds" => 1, "parentGroupId" => 100001, ], ]; $grouped_data = array_reduce( $data, function($res, $el) use ($data) { if ($el["hasChilds"] == 1) { $el["childs"] = array_filter( $data, function($c) use ($el) { echo "$c[parentGroupId] == $el[groupId]" . PHP_EOL; return $c["parentGroupId"] == $el["groupId"]; } ); } $res[] = $el; }, [] ); var_export($grouped_data);
Show:  
Copy Clear