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. 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:

<?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]

Watch a course Learn object oriented PHP

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

<?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]