0

我怎么用树枝做这个?

    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 %}
4

1 回答 1

0

尝试这个..

未经测试!!...

{% 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' %}

这应该工作,但我什至不测试这个..

于 2013-04-29T20:08:25.080 回答