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

Description

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//w ww.j ava 2 s  .c  o  m
    $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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions