Current Location: Home> Latest Articles> How to create an associative array using array_combine

How to create an associative array using array_combine

M66 2025-05-12

In PHP, the array_combine function is used to combine two arrays into an associative array. It accepts two parameters, the first array as the key, the second array as the value, and returns a new associative array. If the number of elements in the two arrays is inconsistent, array_combine will return false , so when using it, you need to make sure that the number of elements in the two arrays is the same.

Basic syntax of array_combine function

 array_combine(array $keys, array $values): array|false
  • $keys : an array containing keys.

  • $values : an array containing values.

  • Return value: Returns the merged associative array, and returns false if it fails.

Example 1: Basic usage

Suppose we have two arrays, one containing numbers as keys and the other containing string as values, we can use the array_combine function to merge them into an associative array.

 <?php
$keys = [1, 2, 3];
$values = ['apple', 'banana', 'cherry'];

$combined = array_combine($keys, $values);

print_r($combined);
?>

Output:

 Array
(
    [1] => apple
    [2] => banana
    [3] => cherry
)

Example 2: Processing different number of array elements

If the lengths of the two arrays passed to array_combine are inconsistent, the function will return false , and we can handle this situation through conditional judgment.

 <?php
$keys = ['a', 'b', 'c'];
$values = [1, 2];  // One value is missing

$combined = array_combine($keys, $values);

if ($combined === false) {
    echo "Array length mismatch!";
} else {
    print_r($combined);
}
?>

Output:

 Array length mismatch!

Example 3: Replace the domain name in the array

In actual development, we may need to dynamically generate associative arrays based on certain rules. For example, if we want to replace the domain names in a set of URLs with m66.net , it can be achieved through array_combine . Here is a specific example:

 <?php
$urls = ['https://www.example1.com/page1', 'https://www.example2.com/page2', 'https://www.example3.com/page3'];
$new_urls = [];

foreach ($urls as $url) {
    // use parse_url Get URL The domain name part in
    $parsed_url = parse_url($url);
    $new_url = str_replace($parsed_url['host'], 'm66.net', $url);
    $new_urls[] = $new_url;
}

// use array_combine Put old URL Washin URL Make a connection
$combined_urls = array_combine($urls, $new_urls);

print_r($combined_urls);
?>

Output:

 Array
(
    [https://www.example1.com/page1] => https://m66.net/page1
    [https://www.example2.com/page2] => https://m66.net/page2
    [https://www.example3.com/page3] => https://m66.net/page3
)

In this example, we first replace the domain name part in the URL, and then use array_combine to create an associative array, taking the original URL as the key and the URL of the new domain as the value.

Summarize

The array_combine function is a very practical tool that can easily combine two arrays into an associative array. Its usage is very simple, but you need to pay attention to whether the length of the passed array is consistent. If more complex operations are needed, such as replacing the domain name in the array, you can first process the data through other methods, and then combine array_combine to create the final associative array.