Current Location: Home> Latest Articles> Detailed Explanation and Usage Example of the array_combine() Function in PHP

Detailed Explanation and Usage Example of the array_combine() Function in PHP

M66 2025-06-12

Detailed Explanation and Usage Example of the array_combine() Function in PHP

In PHP, the array_combine() function is used to merge two arrays into an associative array. One array’s elements serve as the keys, while the other array’s elements become the values, resulting in a combined array. If the two arrays have mismatched lengths, the function will return FALSE.

Syntax

array_combine

Parameters

  • keys - The array of keys.
  • values - The array of values.

Return Value

The array_combine() function returns the merged associative array. If the two arrays do not have the same number of elements, it will return FALSE.

Example

Here is an example that merges two arrays:

<?php
$sports = array('football', 'cricket');
$players = array('david', 'steve');
<p>$res = array_combine($sports, $players);<br>
print_r($res);<br>
?>

Output

The output from running the above code is as follows:

Array
(
    [football] => david
    [cricket] => steve
)

From this example, we can clearly see that the array_combine() function successfully combines the two arrays into an associative array.