0

I am trying to create a layout from an array with Twig. My array has a depth of about seven nests and some keys have been dynamically added (so have different names) such as the time which is a direct child of "PLANNER" and "CLASS_" which has the ID appended to it.

"SCHEDULE": {
    "DAY": "FRIDAY",
    "PLANNER": {
        "830": {
            "CLASS_2231": {
                "INSTRUCTOR": "TEACHER",
                "CLASS_LEVEL": "PRIVATE",
                "MAX_STUDENTS": "2",
                "START_TIME": "830",
                "FINISH_TIME": "900",
                "STUDENTS": [
                    {
                        "STUDENT": {
                            "FIRST_NAME": "First Name",
                            "LAST_NAME": ""
                        }
                    },
                    {
                        "STUDENT": {
                            "FIRST_NAME": "First Name",
                            "LAST_NAME": ""
                        }
                    }
                ]
            },
            "CLASS_1959": {
                "INSTRUCTOR": "TEACHER",
                "CLASS_LEVEL": "PRIVATE",
                "MAX_STUDENTS": "1",
                "START_TIME": "830",
                "FINISH_TIME": "900",
                "STUDENTS": [
                    {
                        "STUDENT": {
                            "FIRST_NAME": "First Name",
                            "LAST_NAME": ""
                        }
                    }
                ]
            }
        },
        "900": {
            "CLASS_1742": {
                "INSTRUCTOR": "TEACHER",
                "CLASS_LEVEL": "DUCKLINGS",
                "MAX_STUDENTS": "4",
                "START_TIME": "900",
                "FINISH_TIME": "930",
                "STUDENTS": [
                    {

I have been trying to loop through this with the following code but it does seem to take variables (that have been set just before the next loop) as part of the for loop condition. However, it will print the variable in the nested loop (should I change the condition to let it run).

--EDITED
    {% for key, value in schedule.SCHEDULE.PLANNER %}
        <div class="scheduleTimeTab">
            <p>{{ key }}</p>
        </div>
        {% set TIME = key %}
        {% for key, value in schedule.SCHEDULE.PLANNER.TIME %}
            <div class="innerSchedule">
                <p>{{ 'IS IT WORKING??' }}</p>
            </div>
        {% endfor %}
    {% endfor %}

I should also add that this code outputs this:

<div class="scheduleContainer">
    <div class="scheduleTimeTab">
        <p>830</p>
    </div>
    <div class="scheduleTimeTab">
        <p>900</p>
    </div>
</div>

Many thanks in advance Michael Z

user3436334
  • 53
  • 1
  • 10
  • 2
    Is `TIME` here `{% for key, value in schedule.SCHEDULE.PLANNER.TIME %}` meant to access the subkey? If so you'd need `{% for key, value in schedule.SCHEDULE.PLANNER[TIME] %}` instead. – Yoshi Nov 17 '17 at 10:42
  • WORKED!! Thanks heaps Yoshi! – user3436334 Nov 17 '17 at 10:48
  • Make it an answer @Yoshi :) – Tokeeen.com Nov 17 '17 at 14:25
  • Possible duplicate of [Twig for loop and array with key](https://stackoverflow.com/questions/10299202/twig-for-loop-and-array-with-key) – a_sarana Nov 27 '17 at 20:00
  • @a_sarana, not a duplicate because as you can see clearly on this question we have a multidimensional array that goes at least 5 levels and the code to extract information from it. – user3436334 Nov 28 '17 at 00:05

0 Answers0