это мое решение:
$categoriesHierchy = [];
foreach ($categories as $mainCategory) {
$childNode = [
'id' => $mainCategory->getID(),
'polishName' => $mainCategory->getPolishName(),
'children' => findChildrenOfCategory($mainCategory)
];
array_push($categoriesHierchy, $childNode);
}
public function findChildrenOfCategory($category)
{
$children = [];
if (count($category->getChildren()) > 0) {
foreach ($category->getChildren() as $child) {
$childCategories = findChildrenOfCategory($child);
$childNode = [
'id' => $child->getID(),
'polishName' => $child->getPolishName(),
'children' => $childCategories
];
array_push($children, $childNode);
}
}
return $children;
}