Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 循环浏览并从细枝中的数组中提取信息_Php_Arrays_Twig_Twig Extension - Fatal编程技术网

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 %}
结果:

约翰先生

以下是工作解决方案:


希望此帮助

非常感谢您的时间,我不知道信息[键]功能是否可用。