The different Between array_merge and array_combine in php

The array_merge function is a built-in function of php. This function is used to combine key and value of two array or more arrays. Between array_merge and array_combine in php.

I have been working at job and the interesting thing is when working with other people’s code  you often learn a pile of material you already didn’t know. A couple of weeks ago I came across a way to merge arrays in PHP with operator, instead of using the array functions (array_merge) so see.

Different Between array_merge and array_combine in php

In this article we will discuss about two function of php (“1”). array_merge(); (“2”). array_combine(). And i will tell you what is the different between array_combine and array_merge in php.

array_combine

It is used to creates a new array by using the key of a array as keys and using the value of another array as values.

FOR EXAMPLE:


<?php

$array1 = array('key1', 'key2', 'key3');

$array2 = array('wholeblogs.com', 'The Whole Blogs', 'wholeblogs');

$new_array = array_combine($array1, $array2);

print_r($new_array);

?>

OUTPUT :

Array([key1]  => wholeblogs.com[key2]    => The Whole Blogs[key2]    =>wholeblogs)

array_merge

It merges one or more than one array such as that the value of one array appended at the end of the first array. If the arrays have the same string keys, then the next value overrides the previous value for that key.

Read more: array_merge VS array_combine

FOR EXAMPLE :


<?php
$a1=array("United Kingdom","USA");
$a2=array("Pakistan","Canada");
print_r(array_merge($a1,$a2));
?>

OUTPUT :

Array ( [0] => wholeblogs.com[1] => USA[2] => Pakistan[3] => Canada)

array_merge and array_combine

Leave a Comment