PHP Tutorial - PHP array_combine() function combines two array to create new associative array






The array_combine() function creates an associative array by using the elements from "keys" array and "values" array.

Note

Both arrays must have equal number of elements!

Syntax

array_combine has the following syntax.

array_combine(keys,values);

Parameter

ParameterIs RequiredDescription
keysRequired.Array of keys
valuesRequired.Array of values

Return

array_combine() function returns the combined array. It returns FALSE if number of elements does not match.





Example

Create an array by using the elements from one "keys" array and one "values" array:


<?php
    $fname=array("Python","Java","HTML","java2s.com");
    $age=array("5","7","3","9");
    
    $c=array_combine($fname,$age);
    print_r($c);
?>

The code above generates the following result.