Sometimes, we want to append one array to another (not array_push or +) with PHP.
In this article, we’ll look at how to append one array to another (not array_push or +) with PHP.
How to append one array to another (not array_push or +) with PHP?
To append one array to another (not array_push or +) with PHP, we can use the array_merge
function.
For instance, we write
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b);
to create 2 arrays $a
and $b
.
Then we call array_merge
with the 2 arrays to return a new array with the entries of both arrays in the same order that they’re passed into the function.
Conclusion
To append one array to another (not array_push or +) with PHP, we can use the array_merge
function.