Caution: This documentation is for eZ Publish legacy, from version 3.x to 6.x.
For 5.x documentation covering Platform see eZ Documentation Center, for difference between legacy and Platform see 5.x Architecture overview.

merge

Summary

Merges input and passed arrays into one array.

Usage

input|merge( array1 [, array2 [, ... ] ] )

Parameters

NameTypeDescriptionRequired
array1 array Array to be merged with the input array. Yes.
array2 array Another array to be merge with the input array. No.

Returns

New array containing all arrays merged.

Description

This operator will merge the input array with all arrays passed as parameters. The resulting array will be returned.

Examples

Example 1

{array( 1, 2 )|merge( array( 3, 4 ), array( 5, 6, 7 ) )}

The following array will be returned: ( 1, 2, 3, 4, 5, 6, 7 ).

Balazs Halasy (05/02/2004 9:33 am)

Balazs Halasy (13/05/2005 12:08 pm)


Comments

  • Hash merging

    This function also works for hashes.
    def $a = hash('a', 'b')
    def $b = hash('c', 'd')
    def $c = $a|merge($b)
    


    will make

    $c['a'] = 'b'
    $c['c'] = 'd'
    


    Very useful for growing hashes iteratively. However, bear in bind that hashes cannot have numbers as keys! I lost an hour trying to figure out why my
    foreach
    
    didn't work. :-)
  • foreach containing a merge

    .. of course my foreach contained a merge.
  • How to set a number as key

    There's a simple solution to set a number as key :

    {def $my_hash=hash()}
     
    {foreach $items as $index=>$item}
     
    {$my_hash=$my_hash|merge(hash(concat('0',$index), $value))}