Array Functions
Advertisements

PHP array_combine() Function

Topic: PHP Array ReferencePrev|Next

Description

The array_combine() function creates an array by using one array for keys and another for its values.

The following table summarizes the technical details of this function.

Return Value: Returns the combined array, FALSE if the number of elements for each array isn't equal.
Changelog: Versions before PHP 5.4.0 issues E_WARNING and returns FALSE for empty arrays.
Version: PHP 5+

Syntax

The basic syntax of the array_combine() function is given with:

array_combine(keys, values)

The following example shows the array_combine() function in action.

<?php
// Sample arrays
$array1 = array("a", "b", "c", "d");
$array2 = array("apple", "ball", "cat", "dog");

// Combining both arrays
print_r(array_combine($array1, $array2));
?>

Parameters

The array_combine() function accepts the following parameters.

Parameter Description
keys Required. Specifies the array of keys to be used.
values Required. Specifies the array of values to be used.

Note: Both the arrays you want to combine using the array_combine() function must have equal number of elements, otherwise it returns FALSE.


More Examples

Here're some more examples showing how array_combine() function basically works:

If the array you want to use for keys has duplicate values, the later value will prevail as key in the combined array, as shown in the following example:

<?php
// Sample arrays
$array1 = array("a", "a", "b", "c");
$array2 = array(1, 2, 3, 4);

// Combining both arrays
print_r(array_combine($array1, $array2));
?>
Advertisements
Bootstrap UI Design Templates