What it is and why to use it
Some data arrives as separate column names and row values. array_combine() pairs values at corresponding positions to create an associative array. The first argument supplies the new keys; the second supplies their values. This can turn a validated import row into readable field lookups.
How it works
The arrays must contain the same number of elements. On PHP 8, unequal counts throw ValueError, which can be caught. Duplicate keys cannot represent separate entries in the resulting map: a later value for the same key replaces an earlier one. Check a schema’s field names and counts before using it for real imports.
Common mistakes and limitations
Do not confuse array_combine with array_merge: combine pairs keys with values instead of appending arrays. Numeric-looking string keys can become integer keys. Untrusted column names still need a known schema and values still need validation before storage.
Run the examples locally
Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.
The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.
These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.
Check your interpreter, lint and run one saved example
php -v
php -l example.php
php example.phpContinue learning
Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.
Syntax
$record = array_combine($columnNames, $rowValues);Examples
Give a row readable field names
Pair a small trusted header with one parsed row.
<?php
declare(strict_types=1);
$columns = ['name', 'language'];
$values = ['James', 'PHP'];
$record = array_combine($columns, $values);
echo $record['name'], ' studies ', $record['language'], PHP_EOL;
- The first position pairs name with James and the second pairs language with PHP.
- The result supports descriptive string-key lookups instead of unexplained numeric offsets.
- Both inputs are already parsed arrays. For actual CSV text, use a CSV parser first because quoted separators are more complex than simple splitting.
- Expected output: James studies PHP
Catch a malformed row
Handle unequal numbers of keys and values without leaving an uncaught exception.
<?php
declare(strict_types=1);
try {
$record = array_combine(['name', 'language'], ['John']);
echo 'Row accepted', PHP_EOL;
} catch (ValueError $error) {
echo 'Row has the wrong number of fields', PHP_EOL;
}
- The header has two entries but the row has only one.
- PHP 8 throws ValueError before assigning a combined result, so the success line is not reached.
- The catch block gives a controlled import error. A batch importer could record the row number and continue according to its chosen policy.
- Expected output: Row has the wrong number of fields
Best practices
- Check row lengths and reject unexpected column names.
- Require unique field names when every column must be retained.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual