Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.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 可以将变量连接到Twig中的if语句吗?_Php_Twig - Fatal编程技术网

Php 可以将变量连接到Twig中的if语句吗?

Php 可以将变量连接到Twig中的if语句吗?,php,twig,Php,Twig,我试图将一个变量连接到一个数组键,以访问Twig中的某些值,但到目前为止没有成功 我有一个大型PHP数组,其中包含如下示例键: $array = [ ... ... ... 'test_1' => $test_1, 'test_2' => $test_2 ]; 我在我的小树枝模板中尝试了以下操作: {% for i in 1..2 %} {% if array.test_{{ i }} != 0 %} <div>Test

我试图将一个变量连接到一个数组键,以访问Twig中的某些值,但到目前为止没有成功

我有一个大型PHP数组,其中包含如下示例键:

$array = [
   ...
   ...
   ...
   'test_1' => $test_1,
   'test_2' => $test_2
];
我在我的小树枝模板中尝试了以下操作:

{% for i in 1..2 %}

   {% if array.test_{{ i }} != 0 %}
      <div>Test</div>
   {% endif %}

{% endfor %}
但这不起作用

有没有办法在Twig中访问这样的值?

试试以下方法:

{% for i in 1..2 %}
    {% if array['test_' ~ i] != 0 %}
        <div>Test</div>
    {% endif %}
{% endfor %}