@Grog2903

Как вывести вложенный массив в twig?

Пытаюсь вывести вложенный массив в twig

Вот массив
array(4) {
  ["03f2d305-35fd-4cf0-8c77-659a8484d844"]=>
  array(5) {
    ["id"]=>
    string(36) "03f2d305-35fd-4cf0-8c77-659a8484d844"
    ["title"]=>
    string(6) "Test 3"
    ["slug"]=>
    string(6) "Test 3"
    ["parent_id"]=>
    NULL
    ["children"]=>
    array(1) {
      ["34906ea1-2039-443a-910c-6ce1d7e8e6fe"]=>
      array(4) {
        ["id"]=>
        string(36) "34906ea1-2039-443a-910c-6ce1d7e8e6fe"
        ["title"]=>
        string(4) "weew"
        ["slug"]=>
        string(4) "weew"
        ["parent_id"]=>
        string(36) "03f2d305-35fd-4cf0-8c77-659a8484d844"
      }
    }
  }
  ["4e9bb859-c93d-43e7-8a06-8860345c44af"]=>
  array(5) {
    ["id"]=>
    string(36) "4e9bb859-c93d-43e7-8a06-8860345c44af"
    ["title"]=>
    string(6) "Test 1"
    ["slug"]=>
    string(6) "Test 1"
    ["parent_id"]=>
    NULL
    ["children"]=>
    array(2) {
      ["a30a1d4c-a495-49d3-97ac-bf8a19649733"]=>
      array(4) {
        ["id"]=>
        string(36) "a30a1d4c-a495-49d3-97ac-bf8a19649733"
        ["title"]=>
        string(6) "Test 2"
        ["slug"]=>
        string(6) "Test 2"
        ["parent_id"]=>
        string(36) "4e9bb859-c93d-43e7-8a06-8860345c44af"
      }
      ["b00d9430-bcec-4e48-ba86-4ea83f1c45a9"]=>
      array(4) {
        ["id"]=>
        string(36) "b00d9430-bcec-4e48-ba86-4ea83f1c45a9"
        ["title"]=>
        string(11) "Hello World"
        ["slug"]=>
        string(11) "Hello World"
        ["parent_id"]=>
        string(36) "4e9bb859-c93d-43e7-8a06-8860345c44af"
      }
    }
  }
  ["9259de78-5275-486e-bc1c-8e1b16e25192"]=>
  array(5) {
    ["id"]=>
    string(36) "9259de78-5275-486e-bc1c-8e1b16e25192"
    ["title"]=>
    string(6) "Test 5"
    ["slug"]=>
    string(6) "Test 5"
    ["parent_id"]=>
    NULL
    ["children"]=>
    array(1) {
      ["abbb5a4d-4e2f-406c-9633-d3c49c530f88"]=>
      array(4) {
        ["id"]=>
        string(36) "abbb5a4d-4e2f-406c-9633-d3c49c530f88"
        ["title"]=>
        string(6) "123312"
        ["slug"]=>
        string(6) "123312"
        ["parent_id"]=>
        string(36) "9259de78-5275-486e-bc1c-8e1b16e25192"
      }
    }
  }
  ["eef6d142-481c-4354-be41-0bf28dbb5961"]=>
  array(4) {
    ["id"]=>
    string(36) "eef6d142-481c-4354-be41-0bf28dbb5961"
    ["title"]=>
    string(4) "2222"
    ["slug"]=>
    string(4) "2222"
    ["parent_id"]=>
    NULL
  }
}


Вот цикл

{% for category in categories %}
                            {{ category['title'] }}
                            {% for child in category['children'] %}
                                ===={{ child['title'] }}
                            {% endfor %}
                       
                {% endfor %}


Получаю такую ошибку:
Key "children" for array with keys "id, title, slug, parent_id" does not exist.

Если в цикле задать вместо children, любое другое значение, то ошибка покажет это:
Key "other" for array with keys "id, title, slug, parent_id, children" does not exist.

Подскажите пожалуйста, что делаю не так
  • Вопрос задан
  • 284 просмотра
Решения вопроса 1
@Grog2903 Автор вопроса
Решил вопрос.

Проблема была в том, что например в главной категории не было подкатегорий, тогда значения в массиве не было и твиг ругался
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы