2017-04-26 157 views
0

如何在循環中將一個數組中的值插入到另一個數組中? 例如將另一個數組中的值替換爲數組twig

{% for day_item in template_string %} 
    <div id="{{lessons.{{day_item.template_string}}.id}}" class="col-lg-2 no-padding col-md-2 col-sm-12 col-xs-12"></div> 
{% endfor %} 
+0

谷歌搜索 「PHP樹枝動態變量」(https://www.google.co.uk/search?q=php+twig+dynamic+variable),最答案似乎圍繞使用屬性函數(https://twig.sensiolabs.org/doc/2.x/functions/attribute.html)。很確定這是你需要的,這個問題只是這些問題的另一個變種?這對你有用嗎? –

回答

1

這是一個演示:

PHP代碼:

// data for twig template 
$data = [ 
    'userIds' => [1, 2, 3], 
    'users' => [ 
     1 => 'Tony', 
     2 => 'Allen', 
     3 => 'Peter', 
    ], 
]; 

樹枝代碼:

{% for userId in userIds %} 
    <li>{{ users[userId] }}</li> 
{% endfor %} 

只是使用[ ]在枝條訪問數組值與小枝變量。

和呈現的HTML是

<li>Tony</li> 
<li>Allen</li> 
<li>Peter</li> 
相關問題