To join arrays in PHP, you can use the array_merge
function, which merges the elements of one or more arrays together.
In this example,
$arr1
and $arr2
with a few elements.array_merge
function to merge the two arrays into a new array named $result
.<?php
$arr1 = array("apple", "banana");
$arr2 = array("cherry", "date");
$result = array_merge($arr1, $arr2);
print_r($result);
?>
Array ( [0] => apple [1] => banana [2] => cherry [3] => date )
In this example,
$arr1
and $arr2
with key-value pairs.array_merge
function to merge the two arrays into a new array named $result
.<?php
$arr1 = array("a" => "apple", "b" => "banana");
$arr2 = array("c" => "cherry", "d" => "date");
$result = array_merge($arr1, $arr2);
print_r($result);
?>
Array ( [a] => apple [b] => banana [c] => cherry [d] => date )
In this tutorial, we learned How to Join Arrays in PHP language with well detailed examples.