# ArrayMerge

This function creates a new array with data from the two passed arrays.

To add all the data from one array into another without creating a new array see the built in function ArrayAppend(arr1, arr2, true).

## Method Signature

```
ArrayMerge(array1=[array], array2=[array], leaveIndex=[boolean])
```

### Arguments

| Argument     | Type      | Required | Description                                                                                            | Default |
| ------------ | --------- | -------- | ------------------------------------------------------------------------------------------------------ | ------- |
| `array1`     | `array`   | `true`   | The first array to merge                                                                               |         |
| `array2`     | `array`   | `true`   | The second array to merge                                                                              |         |
| `leaveIndex` | `boolean` | `true`   | Set to true maintain value indexes - if two values have the same index it will keep values from array1 | `false` |

## Examples

### Standard function syntax

Merge two arrays resulting in a single re-indexed array. All elements of both arrays are preserved.

[Run Example](https://try.boxlang.io/?code=eJxVjjEOwjAMRWdyiihTK%2FUGFSsTiIUNdTDgppFKbTkOqLcnSWFAXp7l52%2BPkoLavb1as3PAPKPrMt1gyVWRBBaPzgy9eaH3AWPR80DpCUrVuYMI6YYkSwVGiF9gRok1YUKYdVoPRI8Sk9dgPaF4bOxYPuns70bbm7cExXNSTtps6oWOIebmL6Yt7gf8AT2c)

```java
fruit = [ 
	"apple",
	"banana",
	"orange"
];
veggies = [
	"tomato",
	"carrot",
	"corn",
	"peas",
	"peppers"
];
healthyFoods = arrayMerge( fruit, veggies );
writeOutput( arrayToList( healthyFoods ) );

```

Result: apple,banana,orange,tomato,carrot,corn,peas,peppers

### Member function syntax

Merge two arrays resulting in a single re-indexed array. All elements of both arrays are preserved.

[Run Example](https://try.boxlang.io/?code=eJxVjbEKAjEMQGf7FaXTHYg%2FcLg6CS5u4hA11yvcNSFNlft726qDZHkhj5dRclC7txdrNg6YZ3TbQjeIZRqSQPTozHUwT%2FQ%2BYKp6OSgtoNScO4iQfpAkNmCE9AVmlNQKE8Ks03ogetTMWL%2FvFhSPnf3V%2B8G8JCiesnLWzpY2rGc6hlSWv0Bf3TeMSzt%2F)

```java
fruit = [ 
	"apple",
	"banana",
	"orange"
];
veggies = [
	"tomato",
	"carrot",
	"corn",
	"peas",
	"peppers"
];
healthyFoods = fruit.merge( veggies );
writeOutput( arrayToList( healthyFoods ) );

```

Result: apple,banana,orange,tomato,carrot,corn,peas,peppers

### Example where leaveIndex parameter is true

Merge two arrays resulting in a single re-indexed array. Where the both arrays have elements in the same position, only values from the first array are included in the result. Valid using standard or member function syntax.

Note how the first three elements of the veggies array are not merged because the fruit array already has values for elements 1-3.

[Run Example](https://try.boxlang.io/?code=eJxVjjsOwjAMhmdyCitTK%2FUGFSsTiIUNdTDgppFKHTkOqLcnSbsgL5%2Fl%2F%2BFRklc4wh3MwWIIM9ku0wOXPBVZcHFkzdCbDznnKRZ5Pii%2FUblqnijCuiHLUiEQxh1CIIk1YSKcdVpPzK8Sk224XkgcNTCWTzrYOzpQSQRtb77ila5JQ9JmM9z47GNe%2FsLaov0BdY4%2FqA%3D%3D)

```java
fruit = [ 
	"apple",
	"banana",
	"orange"
];
veggies = [
	"tomato",
	"carrot",
	"corn",
	"peas",
	"peppers"
];
healthyFoods = arrayMerge( fruit, veggies, true );
writeOutput( arrayToList( healthyFoods ) );

```

Result: apple,banana,orange,peas,peppers

### Additional Examples

```java
aNames = array( 10412, 42, 33, 2, 999, 12769, 888 );
aNames2 = array( 33, "b", "c", "d", "e", "f", "g" );
dump( arrayMerge( aNames, aNames2 ) );
// member function
dump( aNames.merge( aNames2 ) );
dump( aNames.merge( aNames2, true ) );

```

## Related

* [ArrayAppend](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayappend)
* [ArrayAvg](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayavg)
* [ArrayChunk](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraychunk)
* [ArrayClear](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayclear)
* [ArrayContains](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraycontains)
* [ArrayContainsNoCase](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraycontainsnocase)
* [ArrayDelete](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraydelete)
* [ArrayDeleteAt](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraydeleteat)
* [ArrayDeleteNoCase](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraydeletenocase)
* [ArrayEach](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayeach)
* [ArrayEvery](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayevery)
* [ArrayFilter](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfilter)
* [ArrayFind](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfind)
* [ArrayFindAll](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfindall)
* [ArrayFindAllNoCase](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfindallnocase)
* [ArrayFindFirst](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfindfirst)
* [ArrayFindNoCase](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfindnocase)
* [ArrayFirst](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayfirst)
* [ArrayFlatMap](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayflatmap)
* [ArrayFlatten](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayflatten)
* [ArrayGetMetadata](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraygetmetadata)
* [ArrayGroupBy](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraygroupby)
* [ArrayIndexExists](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayindexexists)
* [ArrayInsertAt](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayinsertat)
* [ArrayIsDefined](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayisdefined)
* [ArrayLast](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraylast)
* [ArrayMap](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraymap)
* [ArrayMax](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraymax)
* [ArrayMedian](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraymedian)
* [ArrayMid](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraymid)
* [ArrayMin](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraymin)
* [ArrayNew](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraynew)
* [ArrayNone](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraynone)
* [ArrayPop](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraypop)
* [ArrayPrepend](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayprepend)
* [ArrayPush](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraypush)
* [ArrayRange](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayrange)
* [ArrayReduce](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayreduce)
* [ArrayReduceRight](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayreduceright)
* [ArrayReject](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayreject)
* [ArrayResize](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayresize)
* [ArrayReverse](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayreverse)
* [ArraySet](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayset)
* [ArrayShift](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayshift)
* [ArraySlice](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayslice)
* [ArraySome](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraysome)
* [ArraySort](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraysort)
* [ArraySplice](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraysplice)
* [ArraySum](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraysum)
* [ArraySwap](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayswap)
* [ArrayToList](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraytolist)
* [ArrayToStruct](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraytostruct)
* [ArrayTranspose](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arraytranspose)
* [ArrayUnique](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayunique)
* [ArrayUnshift](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayunshift)
* [ArrayZip](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/array/arrayzip)
