We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Array: add elements

Hi all,

is possible from VOLT templates to add elements to arrays? I've found how to create a new array but no way to add elements to it after creation.

I tried with the following without success: {% SET my_array[] = myObj %}


Not sure if it works, try this as a workaround: {% SET new_array = [new array element] + old_array %} I doubt there is a way to modify existing array.

You can map the "array_push" function with a volt macro:

$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->getCompiler()->addFunction('array_push', 'array_push');

Then feel free to use array_push in your Volt templates:

{{ array_push(my_array, 'new element') }}

my_array will be updated with the "new element" and you can print that somewhere else.