array_combine

Creates an array by using one array for keys and another array for the matching values.

Signature
array_combine(array $keys, array $values): array
  • Added in PHP 5.0.0

Parameters

ParameterTypeDescription
$keysarrayThe array of values to use as keys. Values that are not valid keys are converted following PHP's normal key-casting rules.
$valuesarrayThe array of values to use as the resulting values.

Return value

arrayAn array whose keys come from $keys and whose values come from the element at the same position in $values.

Errors

Throws a ValueError if $keys and $values do not contain the same number of elements.

Changelog

  • PHP 8.0.0Throws a ValueError when the two arrays have different numbers of elements. Previously it raised an E_WARNING and returned false.

array_combine() zips two flat arrays together: the first supplies the keys, the second supplies the values, matched up by position. The keys of the two input arrays are ignored entirely — only their order matters.

When to reach for it

The classic use is a CSV: read the header row once, then combine it with each data row to get named fields instead of $row[0], $row[1]. It is equally handy for pairing a list of column names with a list of bound values, for turning two parallel arrays returned by a legacy API into one associative array, and — together with array_column() — for building lookup maps.

What trips people up

The counts must match exactly. Since PHP 8.0 a mismatch is a ValueError, not a warning and a false return, so an unguarded call on a short CSV row now aborts the script. Check count() first, or catch the error.

Keys go through normal PHP key casting: "7" becomes the integer 7, true becomes 1, null becomes the empty string, and floats are truncated with a deprecation notice. Duplicate keys collapse, with the later value winning — so the result can hold fewer elements than the input.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Turn a CSV header row plus a data row into a named associative record
  • Pair a list of column names with the values fetched from a legacy numeric-index API
  • Build a code => label map from two parallel arrays coming out of a config file

Frequently asked questions

What happens if the two arrays have different lengths?

Since PHP 8.0 array_combine throws a ValueError. In PHP 7 and earlier it emitted a warning and returned false, so code written for PHP 7 that checks for false will now fatal instead.

What is the difference between array_combine and array_merge?

array_combine builds one array from a keys array and a values array by position, while array_merge appends the contents of several arrays together, renumbering integer keys as it goes.

Do the keys of the input arrays matter?

No, only the order of the elements is used. You can pass two arrays with completely different keys and array_combine will still pair the first element with the first, the second with the second, and so on.

More examples

Parsing a whole CSV in memory

Each data line becomes a named record. The output shows two associative arrays with sku/qty/price keys, which is far safer to read than positional indexes when the column order changes.

Example · php
<?php
$lines = ["sku,qty,price", "A-1,4,9.99", "B-2,0,19.50"];
$header = str_getcsv(array_shift($lines));

$records = array_map(fn($l) => array_combine($header, str_getcsv($l)), $lines);
print_r($records);
echo "first sku: ", $records[0]['sku'], "\n";

Duplicate keys collapse

The repeated key 'a' is written twice, so the second write (3) replaces the first. Three values produce only two entries and no warning is raised, which is why counting the result matters when keys are not guaranteed unique.

Example · php
<?php
$keys   = ['a', 'b', 'a'];
$values = [1, 2, 3];

$out = array_combine($keys, $values);
var_dump($out);
echo count($values), " values -> ", count($out), " entries\n";

Key casting rules apply

'5' becomes the integer key 5, true becomes 1, and null becomes the empty string key. The var_dump makes the coercion visible — the keys you get back are not always the values you passed in.

Example · php
<?php
$out = array_combine(['5', true, null, 'ok'], ['a', 'b', 'c', 'd']);

var_dump($out);

Discussion

  • Be the first to comment on this lesson.