W3docs

PHP append one array to another (not array_push or +)

To append one array to another in PHP, you can use the array_merge function.

To append one array to another in PHP, you can use the array_merge function. This function merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

For example:

How to append one array to another in PHP?

<?php

$array1 = array(1, 2, 3);
$array2 = array(4, 5, 6);
$array3 = array_merge($array1, $array2);

print_r($array3);

// $array3 is now [1, 2, 3, 4, 5, 6]

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

Alternatively, you can use the $array[] = syntax to add elements to an array one at a time. For example:

How to append one array to another using $array[] = syntax in PHP?

<?php

$array1 = array(1, 2, 3);
$array2 = array(4, 5, 6);

foreach ($array2 as $element) {
    $array1[] = $element;
}

print_r($array1);
// $array1 is now [1, 2, 3, 4, 5, 6]