foreach($categories as $i => $objCategory)
{
$jsonData[$i]['id'] = $objCategory->id;
$jsonData[$i]['name'] = $objCategory->name;
$jsonData[$i]['image'] = $objCategory->image;
$jsonData[$i]['editors'] = $objCategory->editors;
$jsonData[$i]['can_edit'] = $objCategory->canEdit;
$cnt = 0;
$jsonData[$i]['services'] = array();
foreach($services as $k => $objService)
{
if($objService->mainCategory == $objCategory->id || in_array($objCategory->id, $objService->additionalCategories))
{
if(!empty($objCategory->sortingServices))
{
foreach($objCategory->sortingServices as $index => $serviceId)
{
if($objService->id == $serviceId)
{
$cnt = $index;
break;
}
}
}
$jsonData[$i]['services'][$cnt]['id'] = $objService->id;
$jsonData[$i]['services'][$cnt]['name'] = $objService->name;
$jsonData[$i]['services'][$cnt]['favorite'] = $objService->favorite;
$jsonData[$i]['services'][$cnt]['main_category'] = $objService->mainCategory;
$jsonData[$i]['services'][$cnt]['tags'] = $objService->tags;
$jsonData[$i]['services'][$cnt]['can_read'] = $objService->canRead;
$jsonData[$i]['services'][$cnt]['can_edit'] = $objService->canEdit;
$jsonData[$i]['services'][$cnt]['can_order'] = $objService->canOrder;
++$cnt;
}
}
ksort($jsonData[$i]['services']);
}
$jsonData_i = $jsonData[$i]
в начале цикла.$services[$cnt] = [
'id' => $objService->id,
'name' => $objService->name,
'favorite' => $objService->favorite,
'main_category' => $objService->mainCategory,
'tags' => $objService->tags,
'can_read' => $objService->canRead,
'can_edit' => $objService->canEdit,
'can_order' => $objService->canOrder
];
$jsonData_i['services'] = $services;