-1

i'm new in twig. I had read the twig documentation but can nt find solution, as it is much easier in PHP. ((Trying two array in one loop.))

: i have two arrays one is {{ dump(render_categories) }}--

     array(3) {
       [0] => array(3) {
         [0] => array(2) {
           ["category_id"] => string(2)
           "65" ["name"] => string(11)
           "Annivarsary"
         } [1] => array(2) {
           ["category_id"] => string(2)
           "67" ["name"] => string(9)
           "Christmas"
         } [2] => array(2) {
           ["category_id"] => string(2)
           "66" ["name"] => string(8)
           "Birthday"
         }
       } [1] => array(4) {
         [0] => array(2) {
           ["category_id"] => string(2)
           "61" ["name"] => string(6)
           "Mother"
         } [1] => array(2) {
           ["category_id"] => string(2)
           "62" ["name"] => string(6)
           "Sister"
         } [2] => array(2) {
           ["category_id"] => string(2)
           "60" ["name"] => string(8)
           "Daughter"
         } [3] => array(2) {
           ["category_id"] => string(2)
           "63" ["name"] => string(10)
           "Girlfriend"
         }
       } [2] => array(5) {
         [0] => array(2) {
           ["category_id"] => string(2)
           "73" ["name"] => string(8)
           "Earrings"
         } [1] => array(2) {
           ["category_id"] => string(2)
           "72" ["name"] => string(22)
           "Necklaces&Pendants"
         } [2] => array(2) {
           ["category_id"] => string(2)
           "71" ["name"] => string(5)
           "Rings"
         } [3] => array(2) {
           ["category_id"] => string(2)
           "70" ["name"] => string(9)
           "Bracelets"
         } [4] => array(2) {
           ["category_id"] => string(2)
           "69" ["name"] => string(6)
           "Charms"
         }
       }
     }

Second is {{ dump(load_steps) }} : (of different length)

array(3) {
  [0] => array(4) {
    ["id"] => string(1)
    "1" ["category_id"] => string(2)
    "64" ["heading"] => string(21)
    "What is the occasion?" ["status"] => string(1)
    "1"
  } [1] => array(4) {
    ["id"] => string(1)
    "2" ["category_id"] => string(2)
    "59" ["heading"] => string(5)
    "For which Person" ["status"] => string(1)
    "1"
  } [2] => array(4) {
    ["id"] => string(1)
    "3" ["category_id"] => string(2)
    "68" ["heading"] => string(24)
    "Type text heading 878787" ["status"] => string(1)
    "1"
  }
}

I'm trying to get Output like:

What is the occasion? --(get from second array)--

Annivarsary, Christmas, Birthday --(get from First array)--

For which Person? --(get from second array)--

Mother, Sister, Daughter, Girlfriend --(get from First array)--

------So, on-----

Any help is appreciated, ThankYou.

Anmol singh
  • 158
  • 1
  • 12
  • 1
    Have you tried achieve that in the corresponding controller file and than retrieve this data in twig? – K. B. Jan 23 '21 at 14:29
  • You mean :: to merge both array in controller as new array. and then use it on TWIG? – Anmol singh Jan 23 '21 at 14:31
  • Yes, i'm trying to merge it like this: https://stackoverflow.com/questions/1558291/php-merge-2-multidimensional-arrays But one has 3 multidimension length, other has 2. So, facing some issues. – Anmol singh Jan 23 '21 at 14:31
  • Why not put such logic into the controller? Twig is a templating engine, and such a logic might be put better at other places – Nico Haase Jan 24 '21 at 11:11

1 Answers1

1

Wouldn't this do the trick ?

    {% for key, step in load_steps %}
        {{ step['heading'] }}
        {% for category in render_categories[key] %}
            {{ category['name'] }}
        {% endfor %}
    {% endfor %}
Clément Cartier
  • 174
  • 2
  • 11