0

I need to loop through an multidimensional array in twig.

//EDIT: I still haven't found the solution, i tried the suggestion with attribute, but it did not work.. here you can see the whole code plus error message, maybe someone can help me. I really try to unterstand the problem, but every attempt at a solution fails :/

First i have the array category with different strings.
The strings are user-defined, so they are different depending on the user. E.g.: Car, Food, Sport

The second array array is multidimensional an looks like this:

array(12) {
  ["01"]=>
  array(3) {
    ["Food"]=>float(861)
    ["Car"]=>float(300)
    ["Sport"]=>float(80)
  }
  ["02"]=>
  array(3) {
    ["Food"]=>float(12)
    ["Car"]=>float(199)
    ["Sport"]=>int(0)
  }
  ["03"]=>
  array(3) {
   ["Food"]=>int(0)
    ["Car"]=>int(0)
    ["Sport"]=>float(80)
  }
… 9 more

My Twig Code looks like this

           {% for category in categorys %}
           <tr>
                <th>{{category}}</th>
                 
                 {% for line in array %}  
                    <td> {{ attribute(line, category) }} </td
                 {% endfor %}

            </tr>
            {% endfor %}
         

The final table should look like this:

| Food | 861 | 300 | 80 |

| Car | 12 | 199 |0 |

| Sport | 0 | 0 | 80 |

When i use the attribute functions the errormessage is: Illegal offset type in isset or empty

**TypeError in C:\xampp\htdocs\MMM2\vendor\twig\twig\src\Extension\CoreExtension.php (line 1437)

--> if (((\is_array($object) || $object instanceof \ArrayObject) && (isset($object[$arrayItem]) || \array_key_exists($arrayItem, (array) $object))) || ($object instanceof ArrayAccess && isset($object[$arrayItem]))**

How can i use the variable category in this context? Maybe someone can help me, I don't know how to continue.. Thanks in advance!

2
  • You said you tried using attribute, but it didn't work. Please add that code as well, what didn't work? Commented Jan 22, 2022 at 23:13
  • Does this answer your question? How to access dynamic variable names in twig? Commented Jan 22, 2022 at 23:23

2 Answers 2

0

I'm not totally sure what you're after and I'm not totally sure what your data structure looks like. But if your data looks like this:

$this->render('foo.html', [
    'categories' => [ 'Food', 'Car', 'Sport' ],
    'values' => [
        [ 'Food' => 861, 'Car' => 300, 'Sport' => 80 ],
        [ 'Food' => 12, 'Car' => 199, 'Sport' => 0 ]
    ]
]);

Something like this should work:

<table>
    {% for category in categories %}
        <tr>
            <th>{{ category }}</th>
            {% for line in values %}
                <td>{{ attribute(line, category) }}</td>
            {% endfor %}
        </tr>
    {% endfor %}
</table>

It should render this:

<table>
    <tr>
        <th>Food</th>
        <td>861</td>
        <td>12</td>
    </tr>
    ...
</table>
Sign up to request clarification or add additional context in comments.

8 Comments

My problem is that my data looks like this: array(12) { ["01"]=> array(3) { ["Food"]=>float(861) ["Car"]=>float(300) ["Sport"]=>float(80) } ["02"]=> array(3) { ["Food"]=>float(12) ["Car"]=>float(199) ["Sport"]=>int(0) } … 10 more So there is 1 array with12 arrays in it. And in every array is an array with X elements (in our example Food, Car and Sport). Each Element(key) has one value. And i cant figure out how i can get this values in a loop. I hope now it is more understandable. /EDIT: The array is now in the original question
Why would that be a problem? Works as is - demo
@Bjorn I would add the default filter to account for undefined keys though, which could be the actual problem of OP
@DarkBee Based on the information LittleEast provided it shouldn't be necessary, but could be yes. LittleEast, are you getting any errors? If so, please provide them. I'm afraid we won't be able to help you any further without.
So the error message is: "Illegal offset type in isset or empty" Iam very sure its because of my array. Its not like in your example, because there is one Dimension more. Like: 'values' => [ '01' => [ 'Food' => 861, 'Car' => 300, 'Sport' => 80 ], '02' => [ 'Food' => 12, 'Car' => 199, 'Sport' => 0 ] ...]
|
0

Okay guys, thanks to everyone, especially to Bjorn for the help. I finally found my problem because of a zoomtalk with a friend of mine who is more experienced than me in php.

My problem was the category-array.. This array saved not only texts but entire objects.. so i must add .name to category, that i get the Strings "Food, Car, Sport"...

The answer looks like this

<table>
    {% for category in categories %}
        <tr>
            <th>{{ category }}</th>
            {% for line in values %}
                <td>{{ attribute(line, category.name) }}</td>
            {% endfor %}
        </tr>
    {% endfor %}
</table>

Thanks again for your effort!

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.