Вот пример:
$arResult['SECTIONS'] = [ ['NAME' => 'test1', 'DEPTH_LEVEL' => 1, 'LEFT_MARGIN' => 1, 'RIGHT_MARGIN' => 14], ['NAME' => 'test2', 'DEPTH_LEVEL' => 2, 'LEFT_MARGIN' => 2, 'RIGHT_MARGIN' => 5], ['NAME' => 'test3', 'DEPTH_LEVEL' => 2, 'LEFT_MARGIN' => 6, 'RIGHT_MARGIN' => 13], ['NAME' => 'test4', 'DEPTH_LEVEL' => 3, 'LEFT_MARGIN' => 7, 'RIGHT_MARGIN' => 12], ['NAME' => 'test5', 'DEPTH_LEVEL' => 3, 'LEFT_MARGIN' => 8, 'RIGHT_MARGIN' => 11], ['NAME' => 'test6', 'DEPTH_LEVEL' => 3, 'LEFT_MARGIN' => 9, 'RIGHT_MARGIN' => 10], ['NAME' => 'test7', 'DEPTH_LEVEL' => 2, 'LEFT_MARGIN' => 14, 'RIGHT_MARGIN' => 15], ]; function buildTree($items, $left = 1, $right = PHP_INT_MAX, $depth = 1): array { $outputArray = []; foreach ($items as $item) { if ($item['LEFT_MARGIN'] > $left && $item['RIGHT_MARGIN'] < $right && $item['DEPTH_LEVEL'] == $depth) { $item['ITEMS'] = buildTree($items, $item['LEFT_MARGIN'], $item['RIGHT_MARGIN'], $item['DEPTH_LEVEL'] + 1); $outputArray[] = $item; } } return $outputArray; } $arResult['SECTIONS'] = buildTree($arResult['SECTIONS']); |