Home >> PHP >> What is the difference between array_combine() and array_merge() in PHP

What is the difference between array_combine() and array_merge() in PHP

The array_combine() function used for creates an new array by using one array for keys and another for values(Both arrays must have equal number of elements).

Example

$name=array("Ram","Shyam","Ghanshyam");
$age=array("30","32","34");
print_r(array_combine($name,$age));

Output

Array ( [Ram] => 30 [Shyam] => 32 [Ghanshyam] => 34 )

The array_merge() function used for merges one or more arrays.

Example

$array1=array("a"=>"ram","b"=>"shyam");
$array2=array("c"=>"ghanshyam","b"=>"ravi");
print_r(array_merge($array1,$array2));

Output

Array ( [a] => ram [b] => ravi [c] => ghanshyam )

 

Post Your Comment

Next Questions
What are the differences between array_diff(), array_diff_assoc() and array_diff_key()
What is the difference between array_intersect_assoc() and array_intersect_key()
What is difference between array_push() and array_pop()
What is difference between array_product() and array_sum()
What is the difference between session and cookie
What is session & work
What are data types
What are super global variable
$GLOBALS super global variable
$_SERVER super global variable
$_REQUEST super global variable
$_POST super global variable
$_GET super global variable
$_FILES super global variable
$_ENV super global variable
$_COOKIE super global variable
$_SESSION super global variable
Ternary operator
Variable types
What is password encrypt and decrypt function
Difference between echo and print
How can destroy and unset the variable of a session
Function
isset() function
unset() function

Copyright ©2022 coderraj.com. All Rights Reserved.