How to combine or merge array in php

Combine vs Merge array in php

How to combine or merge array in php

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

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

 

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
)

 

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
)
Top