symfony2 for循环不适用于正确的数组

时间:2014-07-02 10:36:39

标签: loops symfony for-loop

情况:

{{ trollArray.1 }} {# returns (string) "someValue1" #}
{{ trollArray.2 }} {# returns (string) "someValue2" #}

但是当我想制作循环时:

<p> loop begin </p>
    {% for key,value in trollArray %}
        <p>
            Key : {{ key }} <br>
            Value : {{ value }}
        </p>
    {% endfor %}
<p> loop end </p>

仅返回:

loop begin
loop end

1 个答案:

答案 0 :(得分:0)

trollArray来自json_encode()......我忘掉了。 所以......我解决了这个问题:

array {# compatible format with twig loop - json_decode($array,true) #}
  '1' => string '1'
  '2' => string '2'
  '3' => string '3'
  '4' => string '4'
  '5' => string '5'
  '6' => string '6'
  '7' => string '7'
  '8' => string '8'
  '9' => string '9'
  '0' => string '0'

object(stdClass) {# incompatible format with twig loop - json_decode($array,false) #}
  public '1' => string '1'
  public '2' => string '2'
  public '3' => string '3'
  public '4' => string '4'
  public '5' => string '5'
  public '6' => string '6'
  public '7' => string '7'
  public '8' => string '8'
  public '9' => string '9'
  public '0' => string '0'