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
!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82
\ No newline at end of file
!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82
H
templates/home.html.twig,3\8\38ac5fc300d52f94b369e87fbb1937abe29f831f
\ No newline at end of file
Q
!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82
\ No newline at end of file
!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82
H
templates/home.html.twig,3\8\38ac5fc300d52f94b369e87fbb1937abe29f831f
\ No newline at end of file
......@@ -8,19 +8,46 @@ use Symfony\Component\Routing\Attribute\Route;
class HomeController extends AbstractController
{
private function uniqElements(array $array): array {
return array_unique($array, SORT_REGULAR);
private function prepareMenu(array $aMenu): array {
$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')]
public function home(): Response
{
$arr = [
['laravel', 'php'],
['codeigniter', 'php'],
['laravel', 'php'],
['c++', 'java'],
$aMenu = [
['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,],
['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 %}
<h1>Сортировка массива:</h1>
<form method="POST">
{{ dump(array) }}
{% for key1, value1 in array %}
{% for key2, value2 in value1 %}
<input type="hidden" name="array[][]" value="{{ value2 }}">
<ul>
{% for value in menu %}
{% if value.depth == 0 %}
<h2>{{ value.name }}</h2>
{% for val in value.submenu %}
<li>{{ val.name }}</li>
{% endfor %}
{% endfor %}
<button type="submit">Отправить массив</button>
</form>
{% endif %}
{% endfor %}
</ul>
{% 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