The kmerge filter merges two arrays together while preserving their keys (including numeric keys).

If the same array key exists in both arrays then the one in the second array will overwrite the first one.

The above will output:

    [1] => One2
    [2] => Two2
    [3] => Three
    [four] => Four string
    [4] => Four
    [5] => Five

You can also use the kmerge filter to add values to an array when you need to use a specific numeric index. The merge filter will not preserve the numeric index, but the kmerge filter will.

For example, assume that you have an array of app items and each one has a date value. You want to create a new array with the timestamp of the date as the array key value. Once you're done you'll sort the new array.


The kmerge filter has the following signature.


Argument Description

The array value to merge with the first array.

It can be a numeric array or a hash (associative array) or a combination of the two.

< Back to the list of filters