Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

php - Managing two different array in one loop TWIG

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.

question from:https://stackoverflow.com/questions/65859207/managing-two-different-array-in-one-loop-twig

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Wouldn't this do the trick ?

    {% for key, step in load_steps %}
        {{ step['heading'] }}
        {% for category in render_categories[key] %}
            {{ category['name'] }}
        {% endfor %}
    {% endfor %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...