Basic Bridge
Basic Bridge

Reputation: 1911

Get value from different inner array twig

How get I get the combined result from two different array i'm using twig template engine

Array:-

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => This is title
            [labels] => Array
                (
                    [0] => This is label-1
                    [1] => This is lable-2
                )

            [price] => Array
                (
                    [0] => 50
                    [1] => 90
                )

            [desc] => great item
            [tags] => item,great
            [time] => 1352129710
        )

)

What I want

What I tried

<ul>
     {% for item in market %}
    <li>{{ item.title }}</li>
    <ul>
         {% for key in item.labels %}
        <li>{{ key }} - **HOW TO DISPLAY PRICE HERE FROM [price]=>Array(..) **</li>
         {% endfor %}
    </ul>
     {% endfor %}
</ul>

Upvotes: 0

Views: 2170

Answers (2)

Pierrickouw
Pierrickouw

Reputation: 4704

You can use the loop variable.

So : {{item.price[loop.index0]}}

Be careful with two things :

  • Use index0 instead of index or you are going to iterate over your array
  • price and labels array must have the same size

Upvotes: 1

Leonov Mike
Leonov Mike

Reputation: 923

You can try use loop.index0 to get array index. Documentation here.

Try following code:

<ul>
 {% for item in market %}
<li>{{ item.title }}</li>
<ul>
     {% for key in item.labels %}
    <li>{{ key }} - {{ item.price[loop.index0] }}</li>
     {% endfor %}
</ul>
 {% endfor %}
</ul>

Upvotes: 1

Related Questions