symfony2 and twig for on sub-array



好吧,我有一个看起来像的数组。

[0] => Array (
  first_name => j,
  last_name => b,
  times => Array(
    [0] => Array(
        [in1] => a date here
        [out1] => a date here
        [in2] => a date here
        [out2] => a date here
    )
    [1] => Array(
        [in1] => another date here
        [out1] => another date here
        [in2] => another date here
        [out2] => another date here
    ))) 

为了 SO 布局,我简化了数组的外观......

这个列表通常会在开始数组中有超过 100 个不同的人,这些人都需要输出到浏览器...... 这很好,我可以做到..

{% for entity in entity %}
<h3>{{ entity.first_name }} {{ entity.last_name }} ( {{ start|date("m/d/Y") }} - {{ end|date("m/d/Y")}} )</h3>
<table = border="1" cellpadding="5" cellspacing="0">
        <thead>
            <tr>
                <th>Date</th>
                <th>In</th>
                <th>Lunch Out</th>
                <th>Lunch In</th>
                <th>Out</th>
                <th>Extra In</th>
                <th>Extra Out</th>
                <th>Total Time</th>
            </tr>
        </thead>
        <tbody>
            {% for times in entity.times %}
            <tr>
                <td>{{ entity.times.daydate|date("M jS Y") }} </td>
                <td>{{ entity.times.in1 is empty ? "" : entity.times.in1|date("h:i A") }}</td>
                <td>{{ entity.times.out1 is empty ? "" : entity.times.out1|date("h:i A") }}</td>
                <td>{{ entity.times.in2 is empty ? "" : entity.times.in2|date("h:i A") }}</td>
                <td>{{ entity.times.out2 is empty ? "" : entity.times.out2|date("h:i A") }}</td>
                <td>{{ entity.times.in3 is empty ? "" : entity.times.in3|date("h:i A") }}</td>
                <td>{{ entity.times.out3 is empty ? "" : entity.times.out3|date("h:i A") }}</td>
                <td>{{ entity.times.totaltime }} Hours</td>
            </tr>
            {% endfor%}
        </tbody>
</table>
{% endfor %}

那是我目前的树枝代码... 我需要帮助的是,因为每个"实体"都有一个 .times 子数组,也需要循环访问。 正确的方法是什么?

{% for entity in entities %}
    <h3>{{ entity.foo }}</h3>
    {% for time in entity.times %}
        <p>{{ time.bar }}</p>
    {% endfor %}
{% endfor %}

我简化了代码,以便更容易看到您正在寻找的概念。

最新更新