PHP array_combine() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_combine() builds a new array by using the values of one array as keys and the values of a second array as values. The two arrays are matched by position.

You use array_combine() when you have two parallel lists, like a list of column names and a list of data, and you want one associative array. It is very common when turning a row of CSV data into a record with named fields.

Syntax

Call array_combine() with the values you need. Start with this line.

PHP
$user = array_combine($keys, $values);
CallWhat it does
array_combine(['a', 'b'], [1, 2])Returns ['a' => 1, 'b' => 2].
array_combine(['x', 'x'], [1, 2])Returns ['x' => 2], because a repeated key keeps the last value.
array_combine([], [])Returns an empty array.

Both arrays must have the same number of items. The first array supplies the keys in order, and the second array supplies the values in order. Keys of the input arrays themselves are ignored.

Library declarations
PHP
array_combine(array $keys, array $values): array

Parameters

ParameterDescription
keysRequired. The array of values that will become the keys. Values that are not valid keys are converted to strings.
valuesRequired. The array of values to use as the values of the new array.

Return Value

Return ValueDescription
arrayThe combined array, with the first array as keys and the second array as values.

PHP Examples

Example 1 — Building a Record From Two Lists

Combine a list of field names with a list of values.

PHP
<?php
$keys = ['name', 'age', 'city'];
$values = ['Asha', 28, 'Pune'];

print_r(array_combine($keys, $values));

How It Works

  1. $keys holds the three field names and $values holds three matching values.
  2. array_combine() pairs the items by position: first with first, second with second.
  3. 'name' gets 'Asha', 'age' gets 28 and 'city' gets 'Pune'.
  4. print_r() shows the final associative array.

Example 2 — Repeated Keys and Empty Arrays

A repeated key keeps only the last value, and two empty arrays combine into an empty array.

PHP
<?php
$dup = array_combine(['a', 'a', 'b'], [1, 2, 3]);
print_r($dup);

$empty = array_combine([], []);
var_dump($empty);

How It Works

  1. The key 'a' appears twice, with the values 1 and 2.
  2. The later value 2 replaces the earlier value 1.
  3. The result therefore has only two items, 'a' and 'b'.
  4. Two empty arrays have matching sizes, so the result is an empty array.

Example 3 — Handling Different Lengths

When the lists have different sizes, PHP 8 throws a ValueError. Catch it to handle the problem.

PHP
<?php
$keys = ['name', 'age'];
$values = ['Asha'];

try {
    array_combine($keys, $values);
} catch (ValueError $e) {
    echo get_class($e), PHP_EOL;
    echo "Sizes differ: ", count($keys), " and ", count($values), PHP_EOL;
}

How It Works

  1. $keys has two items but $values has only one.
  2. array_combine() cannot pair them, so it throws a ValueError.
  3. The catch block runs and prints the class name of the exception.
  4. The counts of both arrays are printed to show why it failed.

Frequently Asked Questions

It creates an array by using one array for the keys and another array for the values. The items are matched by their position.
PHP 8 throws a ValueError. In PHP 7 and older the function gave a warning and returned false.
The last value wins. For example array_combine(['a', 'a'], [1, 2]) returns ['a' => 2].
Yes. In PHP 8 two empty arrays return an empty array. In PHP 7 and older the result was false.
array_combine() uses two arrays, so each key can get a different value. array_fill_keys() uses a single value and gives the same value to every key.

Next: array_count_values()

array_count_values() counts how many times each value appears in an array. It returns a ne.

array_count_values() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful