Go to main content

array_merge

Merge one or more arrays

ARRAY_MERGE

NAME

array_merge - Merge one or more arrays

SYNOPSIS

array_merge(array1, array2,... [, index])

DESCRIPTION

Inserts the elements of one or more arrays together so that the values of one are appended to the end of the previous one, or at the specify index. If, however, the arrays contain the same values, the later value will not overwrite the original value, but will be appended. It returns the resulting array.

This function has been added at version 5.147

PARAMETERS

array1, array2 : list of arrays to merge. This function requires at least 2 parameters

RETURN

Returns the resulting array.

EXAMPLES

       
arr1 = [1,2,3];
arr2 = ['a','b','c'];
arr3 = ['A','B','C'];
res = arr1.array_merge(arr2, arr3);

// res will contain [1,2,3,"a","b","c","A","B","C"]

res = arr1.array_merge(arr2, arr3, 1);

// res will contain [1,"a","b","c","A","B","C",2,3]

a1 = [];
a2 = ["w0","ax"];
r = a1.array_merge(1,a2);// This will throw an error "Last argument must be an integer."

a1 = [1,3];
a2 = ["w0","ax"]; 
a3 = [1, "d","g", "ax"]; 
res = array_merge(a1,1); // no errors as long as it has 2 parameters
// res will contain [1,3]

a1 = [];
a2 = ["w0","ax"];
res = array_merge(a2,a1);
// res will contain ["w0","ax"]

a1 = [1,3];
a2 = ["w0","ax"];
a3 = [1, "d","g", "ax"];
a4 = ["d","ax"];
res = a1.array_merge(a2,a3,a4);
// res will contain [1,3,"w0","ax",1,"d","g","ax","d","ax"]

// Merge with contexts    
arr1 = [{"name": "Luke"}, {"name": "Paul"}];
arr2 = [{"name": "Peter"}];
arr1 = arr1.array_merge(arr2);
arr1;
// {"name":"Luke"},{"name":"Paul"},{"name":"Peter"}]
arr1 = arr1.array_merge(arr2); // The function will not check if the item already exists and will add it again
arr1;
// [{"name":"Luke"},{"name":"Paul"},{"name":"Peter"},{"name":"Peter"}]
    

 

 

SEE ALSO

{{ include("includes/array.sn") }}

AUTHOR

Written by Pierre Laplante and Caroline Laplante, <laplante@sednove.com>

MODIFICATIONS

1.0 2021-02-26 laplante@sednove.com

Edit

© 2024 extenso Inc. All rights reserved.