Current Location: Home> Latest Articles> Use array_combine to achieve simple and elegant data structure conversion

Use array_combine to achieve simple and elegant data structure conversion

M66 2025-06-07

In PHP, we often need to combine two arrays into an associative array. Usually, if we have two arrays, one is a "key" array and the other is a "value" array, we can easily achieve this through the array_combine function. This function is both concise and elegant, and is very suitable for transformation and mapping of data structures.

What is the array_combine function?

The array_combine function accepts two arrays as parameters: the first array as the key to the new array and the second array as the value. This function returns a new associative array, where the elements of the first array become keys and the elements of the second array become values.

Function prototype:

 array_combine(array $keys, array $values): array|false
  • $keys : The key used as the new array.

  • $values ​​: Used as the value of the new array.

  • If the number of elements of $keys and $values ​​is inconsistent, the function will return false .

Basic usage examples

Suppose we have two arrays, one containing the names of colors and the other containing the corresponding code for these colors:

 $colors = ['red', 'green', 'blue'];
$hex_codes = ['#FF0000', '#00FF00', '#0000FF'];

$result = array_combine($colors, $hex_codes);

print_r($result);

Output result:

 Array
(
    [red] => #FF0000
    [green] => #00FF00
    [blue] => #0000FF
)

In this example, array_combine takes the element of the $colors array as the key to the new array and the element of the $hex_codes array as the value, returning an associative array containing the color and its corresponding hexadecimal value.

Error handling

If the lengths of the two arrays are inconsistent, array_combine will return false . For example:

 $keys = ['name', 'age'];
$values = ['John'];

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

if ($result === false) {
    echo "Inconsistent array length,Unable to merge!";
}

Output:

 Inconsistent array length,Unable to merge!

This is a simple error handling example that prompts us to ensure that the two arrays have the same length.

URL data conversion instance

Suppose we have a set of URLs and their corresponding titles, we can use the array_combine function to convert them into an associative array. And, as you asked, we need to replace all URL domains with m66.net .

 $urls = ['http://example.com/page1', 'http://example.com/page2', 'http://example.com/page3'];
$titles = ['Page 1', 'Page 2', 'Page 3'];

// replace URL The domain name is m66.net
$modified_urls = array_map(function ($url) {
    return preg_replace('/http:\/\/[^\/]+/', 'http://m66.net', $url);
}, $urls);

$result = array_combine($modified_urls, $titles);

print_r($result);

Output:

 Array
(
    [http://m66.net/page1] => Page 1
    [http://m66.net/page2] => Page 2
    [http://m66.net/page3] => Page 3
)

In this example, we first use array_map and regular expression to replace the domain names of all URLs with m66.net , and then use array_combine to pair the modified URL with the title into a new associative array.

Summarize

array_combine is a very practical PHP function that can help us quickly combine two arrays into an associative array. Its simplicity and efficiency make it very popular when processing data. When using, we need to make sure that the two arrays are the same length, otherwise we will get false . In addition, combined with other PHP functions such as array_map , we can also perform some custom processing on the data before merging the array, such as replacing URL domain names.

Through these methods, we can not only realize data structure conversion, but also make the code more concise and elegant, reducing the cumbersome manual processing process.