I have this in PHP:
$units = array();
foreach ($popPorts as $port) {
$units[$port->getFrameNo()][$port->getSlotNo()][$port->getPortNo()] = $port->getPortNo();
}
How can I achieve the same in twig?
I have tried this so far:
{% set frames = [] %}
{% for row in object.popPorts %}
{% set frames[row.frameNo][row.slotNo][row.portNo] = row.portNo %}
{% endfor %}
{{ dump(frames) }}
But then I get an error:
Unexpected token "punctuation" of value "[" ("end of statement block" expected).
The output should be like this:
array (size=3)
(frame) 1 =>
array (size=2)
(slot) 1 =>
array (size=4)
0 => (port) 26
1 => (port) 27
2 => (port) 28
3 => (port) 29
(slot) 5 =>
array (size=2)
0 => (port) 31
1 => (port) 34
(frame) 2 =>
array (size=1)
(slot) 3 =>
array (size=1)
0 => (port) 32
(frame) 3 =>
array (size=1)
(slot) 6 =>
array (size=1)
0 => (port) 33
merge()
. But although it is possible, but will lead to a lot of ugly code (you need temporary variables for your keys...). So if you can do it in php and send the result to twig, I would recommend that instead. - jeroen