diff --git a/src/Controller/UniqElementsController.php b/src/Controller/UniqElementsController.php new file mode 100644 index 0000000000000000000000000000000000000000..52161bc2eb4ce2fcd596d9bf47c31f71b64be186 --- /dev/null +++ b/src/Controller/UniqElementsController.php @@ -0,0 +1,23 @@ +<?php + +namespace App\Controller; + +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Attribute\Route; +use Symfony\Component\HttpFoundation\Request; +use function App\Repository\uniqElements; + +class UniqElementsController extends AbstractController +{ + #[Route('/uniq/elements', name: 'app_uniq_elements')] + public function index(Request $request): Response + { + $requestArray = $request->toArray(); + + $return = uniqElements($requestArray); + + return new JsonResponse($return, Response::HTTP_OK); + } +} diff --git a/src/Repository/UniqElementsRepository.php b/src/Repository/UniqElementsRepository.php new file mode 100644 index 0000000000000000000000000000000000000000..27b3d65c4fbc52ebaa209bf51400db3eed5d9449 --- /dev/null +++ b/src/Repository/UniqElementsRepository.php @@ -0,0 +1,29 @@ +<?php +namespace App\Repository; +/** + * Удалить дубликаты, оÑтавив только уникальные Ð·Ð½Ð°Ñ‡ÐµÐ½Ð¸Ñ + * @param array $array + * @return array + */ +function uniqElements(array $array): array { + $splitedArray = []; + + foreach ($array as $tuple) { + $key = $tuple[0]; + $item = $tuple[1]; + $splitedArray[$key][] = $item; + } + + $returnableArray = []; + foreach ($splitedArray as $key => $item){ + $item = array_unique($item); + foreach ($item as $oneOfItem){ + $returnableArray[] = [ + $key, + $oneOfItem + ]; + } + } + + return $returnableArray; +}