I have an array, but I need to restructure it. I can only do this in twig.
Original array element looks like $arr[0]
:
"_id" => array:3 [
"cityName" => "someCityName"
"id" => 111
"className" => "someClassName"
]
"count" => 85
my result array element should look like $arr[0]:
"someCityName" => [
12 => [
"someClassName" => 32,
"someOtherClassName" => 44
]
]
in php I would just do
$arr[$cityName][$id][$className] = $count;
And this would work, but this must be done in twig.
I was hoping to use twig merge, but when I try doing
{% for infoArr in result %}
{% set cityName = infoArr['_id']['cityName'] %}
{% set id = infoArr['_id']['id'] %}
{% set class = infoArr['_id']['className'] %}
{% set countCity.cityName = countCity.cityName|merge({(id):([])}) %}
{% endfor %}
or
{% set countCity[cityName] = countCity[cityName]|merge({(id):([])}) %}
the error is Unexpected token "punctuation" of value "[" ("end of statement block" expected)
.
How should this be done in twig correctly?