简体   繁体   中英

Twig loop for multidimansial array

I should loop multidimansial array in Twig

I have a multidimansial array which I should loop in twig template. That comes enough tricky I need get the second array value by key. I get first Index which is date and by second loop I want to get value of key amount if value of name_pay equal to Click. I think better will be understandable through my code.

Array

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

Try to use different key name for a second loop. Like this:

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

If dumped array represents reports variable in twig than comes the question: where did you get key 1: reports[1]?

I think first loop should start with

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

Then you will have the date of the report in 'date' key and the list of reports in load_reports variable, which you obviously can walk through by simple loop:

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

Your 'if' condition will look like this then:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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