从不同的内部阵列树枝获取价值

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

  • This is title
    • This is label-1 50
    • This is lable-2 90

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>

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

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>