Current Location: Home> Latest Articles> Merge two lists into dictionary form: array_combine

Merge two lists into dictionary form: array_combine

M66 2025-06-07

In PHP, the array_combine() function is very suitable for combining two arrays into an associative array (i.e., dictionary). This function takes two parameters: the first is the key array, the second is the value array, and then returns an array with the elements in the first array as the key and the elements in the second array as the values.

Function prototype

 array_combine(array $keys, array $values): array|false
  • $keys : an array of keys, containing all elements used as dictionary keys.

  • $values : an array of values ​​that contain all elements used as dictionary values.

Return value:

  • Returns an associative array with the element in $keys as the key and the element in $values ​​as the value.

  • If the number of elements of the two arrays is not equal, or one of the arrays is empty, the function returns false .

Practical examples

Suppose we have two lists, one with the student's name and the other with the student's grades, which we want to merge into a dictionary to facilitate querying each student's grades.

 <?php

// Student name array
$students = ['Alice', 'Bob', 'Charlie', 'David'];

// Student grade array
$scores = [85, 92, 78, 88];

// use array_combine Combine two arrays into a dictionary
$student_scores = array_combine($students, $scores);

// Output result
if ($student_scores !== false) {
    echo "Student achievement dictionary:\n";
    print_r($student_scores);
} else {
    echo "Inconsistent array length,Unable to merge!\n";
}

?>

Code parsing

  1. We first define two arrays: $students stores the student's name, and $scores stores the corresponding scores.

  2. Then use array_combine() to combine the two arrays into a dictionary, the key is the student's name and the value is the student's grade.

  3. If the merge is successful, the return is an associative array, the format is as follows:

     Array
    (
        [Alice] => 85
        [Bob] => 92
        [Charlie] => 78
        [David] => 88
    )
    
  4. If the two arrays are not the same length, array_combine() returns false , so we want to check the results to avoid errors.

Error handling and precautions

  1. The array length is inconsistent : If the lengths of the two arrays are different, array_combine() will return false . In practical applications, we usually need to check the length of the array first, or make sure that they are the same before calling.

  2. Empty array : If any array is empty, the function will also return false . When processing data, be sure to make sure the incoming array is not empty.

  3. Key Uniqueness : The combined dictionary key must be unique. If there are duplicate keys, the following values ​​will override the previous values.

Example 2: Check the array length

To avoid errors due to inconsistent array lengths, we can manually check the lengths of two arrays before using array_combine() :

 <?php

$students = ['Alice', 'Bob', 'Charlie', 'David'];
$scores = [85, 92, 78];

// Check whether the array length is consistent
if (count($students) === count($scores)) {
    $student_scores = array_combine($students, $scores);
    print_r($student_scores);
} else {
    echo "The length of the student list is inconsistent with the grade list,Unable to merge!\n";
}

?>

Common application scenarios

  • Pairing data : array_combine() is a very efficient tool when you need to pair two related arrays (such as names and phone numbers).

  • Form processing : When receiving form data, there may be an array of field names and an array of field values. You can use array_combine() to merge them to form an associative array that is easy to operate.

summary

The array_combine() function is very suitable for combining two corresponding ones into an associative array (dictionary), and is often used in scenarios such as pairing data and form processing. When using it, make sure that the lengths of the two arrays are consistent, otherwise the function will return false and make sure that the key is unique.