Php 循环浏览并从细枝中的数组中提取信息
我目前有一个充满预填充表单字段的数组:Php 循环浏览并从细枝中的数组中提取信息,php,arrays,twig,twig-extension,Php,Arrays,Twig,Twig Extension,我目前有一个充满预填充表单字段的数组: $fields = array('title','first_name') $info = array( 'title' => 'Mr', 'first_name' => 'John', 'last_name' => 'Smith' ) 正如您所看到的,这个特殊的字段数组只包含标题和名字 我的目标是在字段数组中循环,看看我的$info数组中是否有任何信息可以预先填充字段 比如: foreach (fields
$fields = array('title','first_name')
$info = array(
'title' => 'Mr',
'first_name' => 'John',
'last_name' => 'Smith'
)
正如您所看到的,这个特殊的字段数组只包含标题和名字
我的目标是在字段数组中循环,看看我的$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 %}
但很明显,在Twig中,我现在有这样的东西:
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 %}
{%用于键,字段位于context.contenttype.fields%}
{%if key in context.content | keys%}此示例转储您需要的内容:
{% 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 %}
结果:
约翰先生
以下是工作解决方案:
希望此帮助非常感谢您的时间,我不知道信息[键]功能是否可用。