Difference between array_merge and array_combine in php

What is difference between function array_merge() and array_combine() in php


Function array_merge() merges the elements of one or more than one array such that the value of one array appended at the end of first array. If the arrays have same strings key then the later value overrides the previous value for that key.

<?php $array1 = array(“course1” => “java”,”course2″ => “sql”);
$array2 = array(“course1” => “php”,”course3″ => “html”);
$result= array_merge($array1, $array2);
print_r($result); ?>

Result
Array
(
[course1] => php
[course2] => sql
[course3] => html
)


Function array_combine() is used to creates a new array by using the key of one array as keys and using the value of other array as values. One thing to keep in mind while using array_combine() that number of values in both arrays must be same.

<?php $array1 = array(“course1″,”course2″,”course3”);
$array2 = array(“php”,”html”,”css”);
$result = array_combine($array1, $array2);
print_r($result); ?>

Result
Array
(
[course1] => php
[course2] => html
[course3] => css
)

If you have any query or suggestions, feel free to ask me via the comment section below.

1 Comment

Leave a Reply