Backend Development 4 min read

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 :

<code>array_combine(array $keys, array $values) : array</code>

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

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

Output:

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

Updating an Associative Array

<code>$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);
</code>

Output:

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

Processing Form Submission Data

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

Converting a Two‑Dimensional Array to an Associative Array

<code>$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);
</code>

Output:

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

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

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

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.

backendPHPcode 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

login 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.