0

Hi I need to build a table with twig from a php array, I would like to use the array keys as header for each column.

this is the array

    Array ( 
     [0] => Array ( 
         [building_code] => 2C 
         [building_unit_id] => 57 
         [address] => Via monteverde 45 
         [name] => Andrea 
     ) 
     [1] => Array ( 
         [building_code] => 4E 
         [building_unit_id] => 55 
         [address] => Via monteverde 45 
         [name] => Andrea 
     ) 
    )

This is what I've tried so far

<table>
    <thead>
        <tr>

            {% for titolo in prova|keys %}

            <th>{{titolo}}</th>

            {% endfor %}

        </tr>
    </thead>
    <tbody>

        {% for sub_array in prova %}
        <tr>
            {% for value in sub_array %}
            <td>{{ value }}</th>
                {% endfor %}
        </tr>
        {% endfor %}
    </tbody>
</table>

This is the result

0   1
2C  57  Via monteverde 45   Andrea
4E  55  Via monteverde 45   Andrea
8
  • Have you tried anything so far? Commented Jul 28, 2019 at 19:25
  • What you describe sounds good. The next step for you is to take a look at the documentation of the tool you want to use. And maybe at a few examples you can find online. And then you start. Commented Jul 28, 2019 at 19:26
  • You want to use array keys as column headers? Commented Jul 28, 2019 at 19:27
  • Hi @VinayPatil yes this is what I want Commented Jul 28, 2019 at 19:29
  • 1
    possible duplicate of stackoverflow.com/q/51910401/1483629 Commented Jul 28, 2019 at 19:32

2 Answers 2

2

Your issue is that the keys you want to use as headers exist as the keys in the second-depth array, not the primary array. Meaning that you have to loop those keys instead of the keys in the first array.

Using prova.0 to get the first element, will then result in the keys being of the sub-array - which you can then loop as before.

{% for titolo in prova|first|keys %}
    <th>{{titolo}}</th>
{% endfor %}
Sign up to request clarification or add additional context in comments.

6 Comments

Hi @Qirel many thanks it works, accepted your answer because you also explained whay my code wasn't working and I've learnt something knew thanks to you
Consider using prova | first | keys over prova.0 | keys though
I actually had that first, but wasn't sure if you could do it like that way, so I edited it to what I knew would work - I'm not that familiar with twig. Good to know that prova|first|keys would work too!
Yeah, it's safer to use as the key 0 maybe doesn't exist.
You're absolutley right, I just wasn't 100% sure if it was allowed syntax in twig :-) Thanks, DarkBee!
|
2

You can do something like this by fetching keys of the first array of prova

<table>
    <thead>
        <tr>
            {% for titolo in prova.0|keys %}
                <th>{{titolo}}</th>
            {% endfor %}
        </tr>
    </thead>
    <tbody>
        {% for sub_array in prova %}
            <tr>
                {% for value in sub_array %}
                    <td>{{ value }}</th>
                {% endfor %}
            </tr>
        {% endfor %}
    </tbody>
</table>

Comments

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.