array_merge(): This function is used to join the one or more array into single array.
Syntax: array_merge($array1, $array2, $array3...);
Its required to pass at least one array to array_merge().
<?php
$array1 = array("php","web");
$array2 = array("application","service");
print_r(array_merge($array1, $array2));
//Output:
Array ( [0] => php [1] => web [2] => application [3] => service )
?>
If more than one array elements have the similar index key then the last one index overrides the others.
<?php
$array1 = array("key_1"=>"php", "key_2"=>"web");
$array2 = array("Key_3"=>"application", "key_2"=>"service", "key_5"=>"version");
print_r(array_merge($array1,$array2));
//Output:
Array ( [key _1] => php [key _2] => service [key _3] => application [key _5] => version )
?>
If we pass a single numeric array to the array_merge() function, it will return re-indexed numeric array with index starting from zero
<?php
$array = array(5=>"php", 7=>"web", 9=>"application",);
print_r(array_merge($array));
//Output:
Array ( [0] => php [1] => web [3]=> application )
?>
array_merge_recursive(): It also joins the single or more array into single array.
Syntax: array_merge_recursive($array1, $array2, $array3...);
<?php
$array1 = array("key_1"=>"php", "key_2"=>"web");
$array2 = array("Key_3"=>"application", "key_2"=>"service", "key_5"=>"version");
print_r(array_merge_recursive($array1,$array2));
//Output:
Array ( [key _1] => php [key _2] => array( [0] => web [1] =>service ) [key _3] => application [key _5] => version )
?>
If we pass single array to the array_merge_recursive() function, it will work like the array_merge() function.
array_combine(): This function joins two equal arrays into single array by using the elements from one array as keys and other array elements as value.
Syntax: array_combine($array_elements_for_keys, $array_elements_for_values);
<?php
$ array_elements_for_keys = array("php", "web", "application");
$ array_elements_for_values = array("service", "version", "five");
$result = array_combine($array_elements_for_keys, $array_elements_for_values)
print_r($result);
//Output:
Array ( [php] => service [web] => version [application] => five )
?>
Both arrays are required and must have same count
0 Comment(s)