We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to merge array in volt

Hi all

Volt do have support merge array ?

    {% set menuItems = ['organization': 'Organizations', 'project': 'Projects', 'job': 'Jobs'
    ]%}
    {% set menuAdmin = ['user': 'Users', 'role': 'Roles',  'project': 'Projects','template' : 'Email Templates' ,'configuration' : 'Configurations'
    ]%}
    {% if this.session.get('auth')['role']['name'] == 'Superadmin'%}
          {% set menuItems =[ menuAdmin, menuItems ]%}
    {% endif %}

{% set menuItems =[ menuAdmin, menuItems ]%} is that working, but is two element


Array
(
    [0] => Array
        (
            [user] => Users
            [role] => Roles
            [task] => Tasks
            [tag] => Tags
            [project] => Projects
            [template] => Email Templates
            [configuration] => Configurations
        )

    [1] => Array
        (
            [organization] => Organizations
            [project] => Projects
            [job] => Jobs
        )

)

I want look like

    Array
(
    [user] => Users
    [role] => Roles
    [task] => Tasks
    [tag] => Tags
    [project] => Projects
    [template] => Email Templates
    [configuration] => Configurations
    [organization] => Organizations
    [project] => Projects
    [job] => Jobs
)

Thanks all



6.6k

You can create a volt extension which provides this functionality.

Check this: https://docs.phalcon.io/en/latest/reference/volt.html#extending-volt

use the following;

{% set myArray = myArray | merge({ "key" : "value" }) %}