Understanding PHP's array_combine() Function: Syntax, Use Cases, and Code Examples

This article explains PHP's array_combine() function, detailing its syntax, parameters, return values, error handling, and demonstrates multiple practical applications such as creating and updating associative arrays, processing form data, and converting two‑dimensional arrays, with complete code examples.

php Courses
php Courses
php Courses
Understanding PHP's array_combine() Function: Syntax, Use Cases, and Code Examples

PHP provides the array_combine() function to merge two arrays—one of keys and one of values—into a single associative array. The function returns the new array or false if the input arrays differ in length, logging an error.

Syntax :

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

The $keys parameter is an array of keys, and $values is an array of corresponding values. When the arrays have equal length, the function produces a new array where each key from $keys maps to the value at the same position in $values.

Use Cases

Creating a New Associative Array

$keys   = array('name', 'age', 'sex');
$values = array('Tom', '25', 'male');
$arr    = array_combine($keys, $values);
print_r($arr);

Output:

Array
(
    [name] => Tom
    [age]  => 25
    [sex]  => male
)

Updating an Associative Array

$arr1 = array('name'=>'Tom', 'age'=>25, 'sex'=>'male');
$arr2 = array('age'=>26);
$new_arr = array_combine(array_keys($arr1), array_replace($arr1, $arr2));
print_r($new_arr);

Output:

Array
(
    [name] => Tom
    [age]  => 26
    [sex]  => male
)

Processing Form Submission Data

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $keys   = array_keys($_POST);
    $values = array_values($_POST);
    $arr    = array_combine($keys, $values);
    // further processing of $arr
}

Converting a Two‑Dimensional Array to an Associative Array

$data = array(
    array('name'=>'Tom',  'age'=>25),
    array('name'=>'Jerry','age'=>30),
    array('name'=>'Kate', 'age'=>28)
);
$new_data = array();
foreach ($data as $item) {
    $new_data[] = array_combine(array_keys($item), array_values($item));
}
print_r($new_data);

Output:

Array
(
    [0] => Array
    (
        [name] => Tom
        [age]  => 25
    )

    [1] => Array
    (
        [name] => Jerry
        [age]  => 30
    )

    [2] => Array
    (
        [name] => Kate
        [age]  => 28
    )
)

In summary, mastering array_combine() enhances PHP developers' ability to efficiently build and manipulate associative arrays across various scenarios, from simple mappings to complex data transformations.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPCode Examplesassociative arrayarray_combine
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.