top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How array_merge and array_combine differs from each other?

0 votes
451 views

How array_merge and array_combine differs from each other?

posted Jul 2, 2014 by Karamjeet Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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" => "dotnet","course2" => "sql"); $array2 = array(("course1" => "php","course3" => "html"); $result = array_merge($array1, $array2); print_r($result); ?>
OUTPUT :
array
(
[course1] => php
[course2] => sql
[course3] => html
)

Array_combine creates a new array by using the key of one array as keys and using the value of other array as values.
<?php $array1 = array("course1","course2"); $array2 = array(("php","dotnet"); $new_array = array_combine($array1, $array2); print_r($new_array); ?>
OUTPUT :
array
(
[course1] => php
[course2] => dotnet
)

answer Jul 4, 2014 by Rahul Mahajan
...