There are several ways to concatenate arrays in php. The main functions array_merge() and array_merge_recursive() operate on complex arrays. The implode() method handles multidimensional arrays. There is a way to concatenate array elements and combine elements of one array into a string.
Array_merge() function
This function in php combines arrays into a new separate array. The elements of the second array are at the end of the first. If the processed arrays have the same keys, the union will occur as follows. If the keys are string elements, the elements are replaced, and each subsequent value replaces the previous one.
If the elements with the same key are numeric, then each element will be added to the end of the array. As a result, the values will be sorted by keys in ascending order.

The elements in the new array are lined up in such a way that the zero element will be the value with the color key, then the numbers 2 and 4, after them a, b. They are followed by shape=> trapezoid and 4. You should pay attention to the elements withcommon key. In this case, it remains green.
Execution:
Array ([color]=> green [0]=> 2 [1]=> 4 [2]=> a [3]=> b [shape]=> trapezoid [4]=> 4)
Second example how to merge arrays in php.

All elements are stored here, except for those that have a common key of 3. In this case, the value of the first array remains. Result:
array(5) { [0]=> string(6) "zero_a" [2]=> string(5) "two_a" [3]=> string(7) "three_a " [1]=> string(5) "one_b" [4]=> string(6) "four_b" } array_merge_recursive()
Array_merge_recursive() function
This function allows you to recursively concatenate arrays in php. The values of one array go to the end of another. Joining occurs in the same way as in the course of the array_merge() function. The main difference is that absolutely all values are taken into account, including nested multidimensional arrays.

In this case, array elements with string keys are combined. In this case, the following elements that have a common string key favorite are also combined. The numbers 5, 10 are sequentially added to the new array. The element color=> array(…) becomes the beginning of the resulting array, then comes 5 and 10. In this case, the blue value is removed, leaving red and green. Result:
Array ([color]=> Array ([favorite]=> Array ([0]=> red [1]=> green) [0]=> blue) [0]=> 5[1]=> 10)
Implode() function
To concatenate an array in php into a string, use the implode() method. Full syntax:
string implode (string $glue, array $pieces)
The $glue string is empty by default and is optional. The $pieces array represents the elements that will eventually be combined. As a result, a string with array elements is returned, between which there is a separator $glue. This way:
$pieces[0]. $glue. $pieces[1]. $glue. $pieces[2]
Conventionally, the implode() function can be called the opposite of explode(), which splits the string into elements and moves them into an array.

Here the array elements 'name', 'mail' and 'phone' will be combined into the string $comma_separated. ", " is used as a separator between elements. Thus, the result is the string “name, mail, phone number”.
Result
Three ways to merge two arrays in php are:
- array_merge();
- array_merge_recursive();
- implode().
The second method differs from the first one in that the union is recursive. That is, when merging multidimensional arrays, the elements of each nested array will be attached to each other. As a result, in both cases, a new array is formed, consisting of the elements of the two processed. During execution, such features appear in which the values of one array are replaced by elements of another array. In otherscases, it is possible that elements are simply added to the end of the array.
The implode() function concatenates the elements of two arrays into a string. In the arguments, you can specify a character that will separate words.