PHP Array Combine Function
The array_combine function in PHP is a powerful tool for combining two arrays into a single, associative array. This function takes two arrays as arguments, one
The array_combine() function in PHP merges two flat arrays into one associative array: it takes the values of the first array as the keys and the values of the second array as the values. It is the go-to tool when you have two parallel arrays — for example, a list of column names and a list of cell values that line up position-by-position — and you want to zip them into a single key/value map.
This page covers the syntax, what each parameter and the return value mean, the rules you must follow (equal length, valid key types), and the most common real-world patterns.
Syntax
array_combine(array $keys, array $values): array| Parameter | Description |
|---|---|
$keys | Array whose values become the keys of the result. Its values must be valid array keys (integers or strings). |
$values | Array whose values become the values of the result. |
Return value: a new associative array built by pairing each element of $keys with the element of $values at the same position.
Since PHP 8.0, passing arrays of different sizes throws a ValueError. In PHP 7.x and earlier the function instead returned false and emitted a warning.
Basic usage
The classic use case is turning two parallel arrays into one map — here, product names paired with their prices.
This will output:
Array
(
[Product 1] => 10
[Product 2] => 20
[Product 3] => 30
)Combination is done by position, not by sorting or matching — the first key pairs with the first value, the second with the second, and so on.
Building a row from headers and data
A frequent practical use is reconstructing an associative record from a CSV-style header row plus a data row. This is exactly how you might map one line of a parsed CSV file.
<?php
$headers = array("id", "name", "email");
$row = array(101, "Ann", "[email protected]");
$record = array_combine($headers, $row);
echo $record["name"] . " <" . $record["email"] . ">";This outputs:
Ann <[email protected]>Rules and gotchas
Keep these constraints in mind:
- Equal length is required.
$keysand$valuesmust have the same number of elements. On PHP 8+ a mismatch throws aValueError; guard withcount($keys) === count($values)if the lengths are not known in advance. - Keys must be valid. Values used as keys must be integers or strings. Floats are truncated to integers, booleans are cast to
0/1, andnullbecomes the empty string"". - Duplicate keys overwrite. If the
$keysarray contains repeated values, later pairs overwrite earlier ones, so the result can be shorter than the input. To get the unique key count first, seearray_unique(). - Only the values are used. The original keys of both input arrays are ignored — only their values matter.
The example below shows a duplicate key collapsing two entries into one:
<?php
$keys = array("a", "b", "a");
$values = array(1, 2, 3);
print_r(array_combine($keys, $values));The last "a" => 3 overwrites the first, so the output is:
Array
(
[a] => 3
[b] => 2
)Related functions
array_merge()— joins arrays by appending values rather than pairing keys to values.array_flip()— swaps the keys and values of a single array.array_keys()andarray_values()— the inverse operation: pull the keys or values back out of an associative array.
Conclusion
array_combine() is the cleanest way to zip two parallel arrays into an associative array, pairing them element-by-element. Remember the two rules that trip people up most: the arrays must be the same length (a ValueError on PHP 8+ otherwise), and duplicate keys silently overwrite each other.