Skip to content
PHP Logo

What are some potential solutions or approaches to merging arrays based on the given example?

One approach to merging arrays in PHP is to use the array_merge() function. This function takes multiple arrays as arguments and returns a new array containing all the elements from the input arrays. Another option is to use the spread operator (...) to merge arrays in PHP. This operator allows you to combine multiple arrays into a single array.

php
// Using array_merge() function
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$mergedArray = array_merge($array1, $array2);

print_r($mergedArray);
```

```php
// Using spread operator
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$mergedArray = [...$array1, ...$array2];

print_r($mergedArray);