0

I'm relatively new in PHP/Symfony, can you help me with this?

I want to create a CSS tree and print an encapsulated array which contains 6 main categories and n-child categories, structured in this way:

Every array-node contains null-key-array with main-category with name(string) --> contains the category name (1. , 1.1, 1.1.1. ) and n-arrays with sub-nodes. The tree-array is structured by name, contains 6 main-category nodes and n sub- and sub-sub-categories for each node. looks like this:

array:6 [▼
  1 => array:8 [▼
    "" => array:1 [▼
      "mydata" => array:3 [▼
        "id" => 182
        "name" => "1."
        "titel" => "Maincategorie"
      ]
    ]
    1 => array:9 [▼
      "" => array:1 [▼
    "mydata" => array:3 [▼
      "id" => 1
      "name" => "1.1."
      "titel" => "Maincategorie - subcat1"
    ]
  ]
  1 => array:1 [▼
    "" => array:1 [▼
      "mydata" => array:3 [▼
        "id" => 2
        "name" => "1.1.1."
        "titel" => "Mainkategorie - subcat1_2"
      ]
    ]
  ]
  2 => array:6 [▶] 
  3 => array:1 [▶]
  4 => array:1 [▶]
  5 => array:1 [▶]
  6 => array:1 [▶]
  7 => array:1 [▶]
  8 => array:1 [▶]
]
2 => array:7 [▶]
3 => array:10 [▶]
4 => array:4 [▶]
5 => array:17 [▶]
6 => array:6 [▶]
7 => array:5 [▶]
]
  2 => array:5 [▶]
  3 => array:7 [▶]
  4 => array:5 [▶]
  5 => array:7 [▶]
  6 => array:6 [▶]
]

My questions are:

  1. Is it possible to call a recursive function in Twig or something like a self-called macro?

  2. Or, do I need a function to set the depth of array first in the controller? In the empty key "", what is my main-category?

  3. What is the best solution to create a tree from an array?

Thanks a lot - Ifgenia.

Jason Roman
  • 8,146
  • 10
  • 35
  • 40
Ifgenia
  • 29
  • 1
  • 6
  • You have to nest the `for` loops to reflect your array structs, it's not so hard than it seems :) (https://twig.symfony.com/doc/2.x/tags/for.html) – Armage Aug 21 '17 at 14:14
  • 2
    Did you try? Post some code – Max P. Aug 21 '17 at 14:17
  • Possible duplicate of [How to render a tree in Twig](https://stackoverflow.com/questions/8326482/how-to-render-a-tree-in-twig) – DarkBee Aug 22 '17 at 05:29
  • i had the same solution with nested for - but it ist not efficient imho, and i still have some issues, if some child have no nested childs...but i test it now and post my final solution. I asked for some better solution, then nested for's and i have also undefined level depth, and some parents has no childs. – Ifgenia Aug 22 '17 at 16:56
  • @DarkBee - i saw this topic, but the solution with nested fors its not really helpful, because i have indefined depth of tree and some child-childs have no childs for example... what i wanted - is recursive function or something like this – Ifgenia Aug 22 '17 at 17:04
  • @Ifgenia you need to use the solution with the macro – DarkBee Aug 22 '17 at 19:15
  • @Ifgenia https://stackoverflow.com/questions/45955614/multi-level-menu-with-twig/45956255#45956255 – DarkBee Aug 30 '17 at 12:49
  • thank you, DarkBee, i will try this aproach. actual quick & dirty solution are still the nested fors – Ifgenia Sep 05 '17 at 09:20

0 Answers0