array_combine()

array_combine() — Creates an array by using one array for keys and another for its values.

Built-in function of PHP 8.5. The manual lists it as available in PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.

Signature

array_combine(array $keys, array $values): array

Parameters

ParameterTypeWhat it takes
$keysarrayArray of keys to be used. Illegal values for key will be converted to string.
$valuesarrayArray of values to be used

Return value

Returns the combined array.

Example

A runnable snippet:

$a = array('green', 'red', 'yellow'); $b = array('avocado', 'apple', 'banana'); $c = array_combine($a, $b); print_r($c);

Output:

Array ( [green] => avocado [red] => apple [yellow] => banana )

Changelog

VersionChange
8.0.0array_combine() will now throw a ValueError if the number of elements for each array is not equal; previously this function returned false instead.

See also

array_merge(), array_walk(), array_values(), array_map()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page array_combine() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.