0

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?

4
  • Don't do this in twig. You would need to reconstruct every part of the multi dimensional array and merge it right back. Write a filter/function to modify the array Commented Aug 13, 2019 at 6:00
  • you can show the reconstruction process? Commented Aug 13, 2019 at 6:09
  • You want a new array actually right? Commented Aug 13, 2019 at 6:30
  • I need an array with different structure, like I described. If it is new or not I do not care, but I suppose it should be new Commented Aug 13, 2019 at 6:53

1 Answer 1

1

If you wanted to do something like this in twig, which I'm not encouraging, you need to reconstruct every part of the array. Also note you can't use merge with numeric indeces, otherwise the data will be appended, not overwritten. More information on this issue here

$data = [
    'data' => [
        [
            '_id' => [
                'cityName'  => 'foo',
                'className' => 'alpha',
                'id'        => 42,
            ],
            'count' => 40,
        ],
        [
            '_id' => [
                'cityName'  => 'bar',
                'className' => 'alpha',
                'id'        => 55,
            ],
            'count' => 123,
        ],
        [
            '_id' => [
                'cityName'  => 'foo',
                'className' => 'beta',
                'id'        => 42,
            ],
            'count' => 99,
        ],
    ],
];

{% set output = {} %}

{% for raw in data %}
    {% if not attribute(output, raw._id.cityName) is defined %}
        {% set output = output|merge({ (raw._id.cityName) : {}, }) %}
    {% endif %}

    {% if not attribute(output[raw._id.cityName], 'id_'~raw._id.id) is defined %}
        {% set output = output|merge({(raw._id.cityName) : output[raw._id.cityName] | merge({('id_'~raw._id.id):[], }) }) %}
    {% endif %}

    {% set output = output|merge({(raw._id.cityName) : output[raw._id.cityName] | merge({('id_'~raw._id.id): output[raw._id.cityName]['id_'~raw._id.id]|merge({(raw._id.className): raw.count}), }) }) %}
{% endfor %}


{{ output.foo.id_42.alpha }} {# output: 40 #}

demo

Sign up to request clarification or add additional context in comments.

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.