Faery
Faery

Reputation: 4650

How to get only one specific element of a multidimensional array in Twig

I have a twodimensional array (if I counted it corectlly), called $all_items. It contains several arrays one for each category of items and each of this arrays contains an item - with category, description and so on. This is my code so far:

    {% for all_i in all_items %}
        <table class="table table-condensed">
        {% for i in all_i %}
            <tr>
                <td>{{ loop.index }}. </td>
                <td>{{ i.description }}</td>
                <td>{{ i.price }}</td>
            </tr>
        {% endfor %}
        </table>
    {% endfor %}

What I want is to display the name of each category and then the items in this category. I'm able to call only {{ i.category }}, but then it would be displayed for each item, not only once.

I was wondering if there is a way to do something like that below and what is the right syntax for this.

    {% for all_i in all_items %}
        <table class="table table-condensed">
        {{ all_i[i].category }}
        {% for i in all_i %}
            <tr>
                <td>{{ loop.index }}. </td>
                <td>{{ i.description }}</td>
                <td>{{ i.price }}</td>
            </tr>
        {% endfor %}
        i++;
        </table>
    {% endfor %}

The value of i is set to be 0 from the Controller and is passed to the template.

The error I get is Key "0" for array with keys "" does not exist

This line works fine in the Controller:

$i = $all_items[0][0]->getCategory();

Could you please help me fix this?


UPDATE

%array% [[!!php/object:O:29:"EM\ExpensesBundle\Entity\Item":7:{s:5:"*id";i:8;s:14:"*description";s:8:"Вода";s:8:"*price";s:4:"5.00";s:7:"*date";O:8:"DateTime":3:{s:4:"date";s:19:"2007-01-01 00:00:00";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/Berlin";}s:8:"*notes";N;s:11:"*category";O:48:"Proxies\__CG__\EM\ExpensesBundle\Entity\Category":4:{s:17:"__isInitialized__";b:1;s:5:"*id";i:1;s:7:"*name";s:28:"Храна и напитки";s:8:"*items";O:33:"Doctrine\ORM\PersistentCollection":2:{s:39:"Doctrine\ORM\PersistentCollectioncoll";O:43:"Doctrine\Common\Collections\ArrayCollection":1:{s:54:"Doctrine\Common\Collections\ArrayCollection_elements";a:0:{}}s:46:"Doctrine\ORM\PersistentCollectioninitialized";b:0;}}s:8:"*month";O:45:"Proxies\__CG__\EM\ExpensesBundle\Entity\Month":7:{s:17:"__isInitialized__";b:0;s:5:"*id";N;s:7:"*name";N;s:9:"*budget";N;s:8:"*notes";N;s:10:"*spended";N;s:8:"*saved";N;}}, !!php/object:O:29:"EM\ExpensesBundle\Entity\Item":7:{s:5:"*id";i:9;s:14:"*description";s:14:

This is a part of what I get when I write

`{{all_items | yaml_dump }}

but I still can't figure out what indexes to use. Honestly, I don't know how to understand this.

Upvotes: 1

Views: 5599

Answers (1)

Mark
Mark

Reputation: 1754

I'm assuming that all of the items in the all_i array have the same category? If so, and the array is not a named key value array then you could just do something like the below to get the first category name from the sub array.

{% for all_i in all_items %}
    {% if all_i|length > 0 %}
        <h3>{{ all_i[0].category }}</h3>
        <table class="table table-condensed">
            {% for i in all_i %}
                <tr>
                    <td>{{ loop.index }}. </td>
                    <td>{{ i.description }}</td>
                    <td>{{ i.price }}</td>
                </tr>
            {% endfor %}
        </table>
    {% endif %}
{% endfor %}

Upvotes: 1

Related Questions