多维数组

时间:2016-06-09 07:04:57

标签: php arrays symfony twig

我应该在Twig中循环多维数组

我有一个多动态数组,我应该在twig模板中循环。这足够棘手我需要按键获取第二个数组值。如果name_pay的值等于Click,我得到第一个是日期和第二个循环的索引,我想获得密钥量的值。我认为通过我的代码可以理解更好。

阵列

Array
(
[2016-05-31 00:00:00] => Array
    (
        [Основной долг] => Array
            (
                [0] => Array
                    (
                        [type_pay] => 0
                        [oper_type] => 4
                        [name_pay] => CLICK
                        [name_oper] => Основной долг
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 1971022
                    )

                [1] => Array
                    (
                        [type_pay] => 1
                        [oper_type] => 4
                        [name_pay] => Наличные
                        [name_oper] => Основной долг
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 564550
                    )

                [2] => Array
                    (
                        [type_pay] => 2
                        [oper_type] => 4
                        [name_pay] => Терминал
                        [name_oper] => Основной долг
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 122714
                    )

            )

        [Депозит] => Array
            (
                [0] => Array
                    (
                        [type_pay] => 1
                        [oper_type] => 3
                        [name_pay] => Наличные
                        [name_oper] => Депозит
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 1175942
                    )

                [1] => Array
                    (
                        [type_pay] => 2
                        [oper_type] => 3
                        [name_pay] => Терминал
                        [name_oper] => Депозит
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 1316410
                    )

            )

        [] => Array
            (
                [0] => Array
                    (
                        [type_pay] => 100
                        [oper_type] => 
                        [name_pay] => Терминал
                        [name_oper] => 
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 843795
                    )

            )

        [Итого] => Array
            (
                [0] => Array
                    (
                        [click_count] => test
                        [bill_count] => test
                        [terminal_count] => test
                        [time_pay] => 2016-05-01
                        [count_all] => test
                        [name_oper] => Итого
                    )

            )

    )

)

HTML TWIG LOOP

{% for key, rep in reports[1] %}
            <tr>

              <td class="collapsing">
                {{key|date("Y/m/d")}}
              </td>
              {% for key, main in rep %}
                  {% for key, qwerty in main %}
                  {% if name_pay == 'CLICK' %}
                  <td>{{ qwerty.amount }}</td>
                  {% endif %}       
                  <td></td>
                  <td></td>
                  {% endfor %}

              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              {% endfor %}
            </tr>

            {% endfor %}

2 个答案:

答案 0 :(得分:0)

尝试为第二个循环使用不同的键名。像这样:

{% for key, rep in reports[1] %}
       <tr>

          <td class="collapsing">
            {{key|date("Y/m/d")}}
          </td>
          {% for mainKey, main in rep %}
            {% if mainKey == 'Основной долг' %}
                {% if main.name_pay == 'CLICK' %}  
                    <td>{{ rep[main.amount] }}</td>
                {% endif %}
          <td></td>
          <td></td>
            {% endif %}

          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          {% endfor %}
        </tr>

        {% endfor %}

答案 1 :(得分:0)

如果dumped数组代表报告中的报告变量而不是问题:你从哪里获得密钥1:报告[1]?

我认为第一个循环应该以

开头
{% for date, loan_reposrt in reports %}
...
{% endfor %}

然后,您将在'date'键中获得报告的日期,并在load_reports变量中显示报告列表,您显然可以通过简单的循环来完成:

{% for report in loan_reports %}
...
{% endfor %}

您的'if'条件将如下所示:

{% if report.name_pay == 'CLICK' %}
...
{% endif %}