Last Update - 28 Apr 2021

How to combine or merge array in php

How to combine or merge array in php

Beginners have lots of confusion between array combine and array merge in PHP. So I just go through with this post to introduce the difference between the array combine and array merge in PHP.

Let's start with these PHP functions which are used for combing and merging the array viz array_combine() and array_merge().

Array Combine

Now talk about the array_combine() :

<?php
$name = array("Jhon","Tony","Hulk");
$age = array("25","34","40");
$result = array_combine($fname,$age);
echo '<pre>'; // used for indenting
print_r($result );
?>

Output of array combine is:

Array
(
    [Jhon] => 25
    [Tony] => 34
    [Hulk] => 40
)

Array Merge

Now talk about the array_merge() :

<?php
$array_one = array("Jhon","Devid");
$array_two = array("Kim","Sara");
echo  '<pre>'; // used for indenting
print_r(array_merge($array_one,$array_two));
?> 

Output of array merge is:

Array
(
    [0] => Jhon
    [1] => Devid
    [2] => Kim
    [3] => Sara
)

Did you find this page helpful? X