G
G
Grog29032020-03-27 13:02:57
symfony
Grog2903, 2020-03-27 13:02:57

How to display nested array in twig?

I am trying to display nested array in twig

Here is the array

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
  }
}


Here is the cycle

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


I get this error:
Key "children" for array with keys "id, title, slug, parent_id" does not exist.

If you set any other value instead of children in the loop, then the error will show this:
Key "other" for array with keys "id, title, slug, parent_id, children" does not exist.

Please tell me what I'm doing wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grog2903, 2020-03-27
@Grog2903

Resolved the issue.
The problem was that, for example, there were no subcategories in the main category, then there was no value in the array and the twig cursed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question