Twig无法从2个合并数组中获取密钥

时间:2017-01-31 01:45:26

标签: php twig

我有两个携带相似数据的数组,我只需要每个数据来自“testimonial_section”键。  我知道如何从其中任何一个获取数据(openGames [#]。testimonial_section),但我如何最好地从两者中获取数据?

string(31) "openGames.testimonial_section"
NULL

string(32) "openGames[0].testimonial_section"
array(1) {
  [0]=>
  array(2) {
    ["testimonial"]=>
    string(32) "
"the kidnapping" Test 1 


"
    ["author"]=>
    string(13) "test author 1"
  }
}

string(32) "openGames[1].testimonial_section"
array(2) {
  [0]=>
  array(2) {
    ["testimonial"]=>
    string(34) "
norcross kidnapping test 1


"
    ["author"]=>
    string(28) "norcross kidnapping author 1"
  }
  [1]=>
  array(2) {
    ["testimonial"]=>
    string(34) "
norcross kidnapping test 2


"
    ["author"]=>
    string(28) "norcross kidnapping author 2"
  }
}

最后我需要一个基于条件的数组,或者有数据的数组,我正在尝试运行类似...

{% if testimonial_section != empty %}
  {% for each_section in testimonial_section %}
  {{ each_section.testimonial }}
  {% endfor %}
{% endif %}

我对twig很新,我在通过文档过滤掉如何做到这一点时遇到了麻烦。希望你能帮忙。

1 个答案:

答案 0 :(得分:0)

试一试

  {% for each_section in openGames %}
      {% if each_section.testimonial_section != empty %}  
         {% for each_testimonial in each_section %} 
           {{ each_testimonial.testimonial }}
         {% endfor %}
       {% endif %}
  {% endfor %}