2015-07-20 24 views
3

我現在有一個數組全預填充表單字段:循環通過拉從陣列信息嫩枝

$fields = array('title','first_name') 

$info = array(
    'title' => 'Mr', 
    'first_name' => 'John', 
    'last_name' => 'Smith' 
) 

正如你可以看到這個特定領域數組只包含標題和名字。

我的目標是循環訪問fields數組,並查看我的$info數組中是否有任何信息用於預填充字段。

喜歡的東西:

foreach (fields as field) { 
    if (field is in $info array) { 
     echo the_field_value; 
    } 
} 

但很明顯在枝條,目前我有類似:

{% for key, field in context.contenttype.fields %} 
    {% if key in context.content|keys %} << is array 
     {{ key.value }}<< get the value of the field 
    {% endif %} 
{% endfor %} 

任何幫助是極大的讚賞。

回答

1

這個例子甩了你需要的東西:

{% set fields = ['title','first_name'] %} 

{% set info = { 'title': 'Mr', 'first_name': 'John', 'last_name': 'Smith' } %} 


{% for key in fields %} 
    {% if key in info|keys %} 
     {{ info[key] }} 
    {% endif %} 
{% endfor %} 

結果:

約翰先生

這裏工作的解決方案:http://twigfiddle.com/i3w2j3

希望這有助於

+2

非常感謝你的時間,我不知道如果信息[鍵]功能。 – jmack

+0

嗨@jmack歡迎您! – Matteo