PHP Function Reference

PHP array_combine() Function



The PHP array_combine() function creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.

Syntax

array_combine(keys, values)

Parameters

keys Required. Specify an array of keys to be used. Illegal values for key will be converted to string.
values Required. Specify an array of values to be used.

Return Value

Returns the combined array, false if the number of elements for each array is not equal.

Exceptions

Throws E_WARNING if the number of elements in keys and values does not match.

Example:

The example below shows the usage of array_combine() function.

<?php
$Arr1 = array("Red", "Green", "Blue", 
              "White", "Black");
$Arr2 = array(1, 2, 3, 4, 5);

//using Arr1 as array of keys and
//Arr2 as array of values
$Arr3 = array_combine($Arr1, $Arr2);

//displaying the result
print_r($Arr3);
?>

The output of the above code will be:

Array
(
    [Red] => 1
    [Green] => 2
    [Blue] => 3
    [White] => 4
    [Black] => 5
)

❮ PHP Array Reference