4

Alright, I've got an array that looks like.

[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
    ))) 

I've simplified the look of the array for the sake of the SO layouts...

This list will often times have over 100 different people in the beginning array which will all need to be output to the browser... Which is fine I can do that..

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

That is my current twig code... What I need help with, is since each "entity" has a .times sub-array that also needs to be looped through.. What is the proper way to do that?

1 Answer 1

4
{% for entity in entities %}
    <h3>{{ entity.foo }}</h3>

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

I've simplified the code so that it's easier to see the concept you are looking for.

Sign up to request clarification or add additional context in comments.

2 Comments

doesn't work... the <p>{{ time.bar }}</p> isnt rendered.. Even with a fresh cache...
it was actually my query that was giving me the trouble not the twig... but this is exactly how to do it, so we'll select the answer for future reference of other people.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.