Skip to content
Snippets Groups Projects
Commit f47c6e5c authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

add prepareMenu

parent c83d1ae6
No related branches found
No related tags found
1 merge request!4Ptps controller 4
Q Q
!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 !src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82
\ No newline at end of file H
templates/home.html.twig,3\8\38ac5fc300d52f94b369e87fbb1937abe29f831f
\ No newline at end of file
Q Q
!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82 !src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82
\ No newline at end of file H
templates/home.html.twig,3\8\38ac5fc300d52f94b369e87fbb1937abe29f831f
\ No newline at end of file
...@@ -8,19 +8,46 @@ use Symfony\Component\Routing\Attribute\Route; ...@@ -8,19 +8,46 @@ use Symfony\Component\Routing\Attribute\Route;
class HomeController extends AbstractController class HomeController extends AbstractController
{ {
private function uniqElements(array $array): array { private function prepareMenu(array $aMenu): array {
return array_unique($array, SORT_REGULAR); $result = [];
foreach ($aMenu as $arr) {
if ($arr['depth'] === 0) {
$result[] = array(
'name' => $arr['name'],
'depth' => $arr['depth'],
'submenu' => []
);
continue;
}
$result[array_key_last($result)]['submenu'][] = array(
'name' => $arr['name'],
'depth' => $arr['depth'],
);
}
return $result;
} }
#[Route('/', name: 'home')] #[Route('/', name: 'home')]
public function home(): Response public function home(): Response
{ {
$arr = [ $aMenu = [
['laravel', 'php'], ['name' => 'Смартфоны и гаджеты','depth' => 0,],
['codeigniter', 'php'], ['name' => 'Смартфоны, мобильные телефоны','depth' => 1,],
['laravel', 'php'], ['name' => 'Планшеты','depth' => 1,],
['c++', 'java'], ['name' => 'Наушники и гарнитуры','depth' => 1,],
['name' => 'Компьютеры и ноутбуки','depth' => 0,],
['name' => 'Ноутбуки и аксессуары','depth' => 1,],
['name' => 'Компьютеры и мониторы','depth' => 1,],
['name' => 'Компьютерные комплектующие','depth' => 1,],
['name' => 'Техника для дома','depth' => 0,],
['name' => 'Техника для уборки','depth' => 1,],
['name' => 'Товары для ухода за одеждой','depth' => 1,],
['name' => 'Аксессуары для техники','depth' => 1,],
['name' => 'Товары для дома и кухни','depth' => 0,],
['name' => 'Посуда','depth' => 1,],
]; ];
return $this->json($this->uniqElements($arr));
return $this->render('home.html.twig', ['menu' => $this->prepareMenu($aMenu)]);
} }
} }
{% block body %} {% block body %}
<h1>Сортировка массива:</h1> <ul>
<form method="POST"> {% for value in menu %}
{{ dump(array) }} {% if value.depth == 0 %}
{% for key1, value1 in array %} <h2>{{ value.name }}</h2>
{% for key2, value2 in value1 %} {% for val in value.submenu %}
<input type="hidden" name="array[][]" value="{{ value2 }}"> <li>{{ val.name }}</li>
{% endfor %} {% endfor %}
{% endfor %} {% endif %}
<button type="submit">Отправить массив</button> {% endfor %}
</form> </ul>
{% endblock %} {% endblock %}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment