PHP array_combine() Function

Example

Create a new array by merging two arrays, where one array element is the key name and the other array element is the key value:

<?php
$fname=array("Bill","Steve","Mark");
$age=array("60","56","31");
$c=array_combine($fname,$age);
print_r($c);
?>

Run Example

Definition and Usage

The array_combine() function creates a new array by merging two arrays, one of which is the key name and the other is the key value.

Note:The number of elements in the key name array and the key value array must be the same!

If one of the arrays is empty, or the number of elements in the two arrays is different, the function returns false.

Syntax

array_combine(keys,values);
Parameters Description
keys Required. Key name array.
values Required. Key-value array.

Tips and Comments

Note:The two parameters must have the same number of elements.

Technical Details

Return Value: Returns the merged array. If the number of elements in the two arrays does not match, it returns FALSE.
PHP Version: 5+
Update Log: Before PHP 5.4 version, if the array is empty, it will return an E_WARNING level error and FALSE.