繁体   English   中英

用树枝缠绕

[英]Looping with twig

我该如何用树枝做这件事?

    foreach($estimate_data AS $year => $month) {
        foreach ($month AS $monthNo => $estimates) {
          $chart_data .= "['". $year ."/".$monthNo."',"; 
          foreach (array(0, 1, 3, 5, 8, 13, 20) AS $est){
              $chart_data .= (isset($estimates[$est]) ? $estimates[$est] : 0) .",";
          }
          $chart_data .= "],\n";
        }
    }

到目前为止,这是我所拥有的,但是我在内部foreach中苦苦挣扎,并检查数组中元素的存在:

      {% for year, month in chart_data %}
        {% for month_no, estimates in month %}
          ['{{year}}/{{month_no}}',
          {% for i in [0, 1, 2, 3, 5, 8, 13, 20]  %}
            {% if estimates %}
            {{estimates[i]}} ,
            {% endif %}
          {% endfor %}
        {% endfor %}
      {% endfor %}

尝试这个..

未经测试!

{% for year, month in estimate_data %}
    {% for month_no, estimates in month %}
        {% set chart_data = chart_data ~ "['" ~ year ~ "/" ~ monthNo ~ "'," %} # concat the strings
        {% for est in [0, 1, 3, 5, 8, 13, 20] %}
            {% set chart_data = chart_data ~ (defined estimates.est) ? estimates.est : 0 ~ ',' %} # if estimates.est is defined
        {% endfor %}
        {% set chart_data = chart_data ~ "],\n" %} # concat the final string.
    {% endfor %}
{% endfor %}

编辑:我想,如果您尝试使用以下方法进行连接:

{% set variable ~= 'text' %}

这应该可以,但是我什至不测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM