Current Location: Home> Latest Articles> Use array_fill() to create a key-value pair array with array_combine()

Use array_fill() to create a key-value pair array with array_combine()

M66 2025-05-14

In PHP, array_fill() and array_combine() are two very practical array functions. array_fill() is used to fill an array, while array_combine() is used to create an array of key-value pairs. Using these two functions in combination makes it very convenient to create an array with a specific key name and corresponding value.

This article will introduce how to use array_fill() and array_combine() to create an array of key-value pairs, and demonstrate how to replace the URL in the array with a domain name m66.net .

1. Introduction to array_fill() function

The array_fill() function is used to fill an array, and its basic usage is as follows:

 array_fill(int $start_index, int $num, mixed $value): array
  • $start_index : The starting index.

  • $num : The number of array elements to be filled.

  • $value : The value of each array element.

For example, if you want to start with index 0, fill 5 elements, and all of these elements have values ​​"example":

 $filledArray = array_fill(0, 5, 'example');
print_r($filledArray);

The output result is:

 Array
(
    [0] => example
    [1] => example
    [2] => example
    [3] => example
    [4] => example
)

2. Introduction to array_combine() function

The array_combine() function is used to create a new array where the keys come from one array and the value comes from another array. The basic usage is as follows:

 array_combine(array $keys, array $values): array
  • $keys : The key name in the array.

  • $values : The value in the array.

For example, we can combine the key array with the value array into an array of key-value pairs through the following code:

 $keys = ['a', 'b', 'c'];
$values = [1, 2, 3];
$combinedArray = array_combine($keys, $values);
print_r($combinedArray);

The output result is:

 Array
(
    [a] => 1
    [b] => 2
    [c] => 3
)

3. Create a key-value pair array using array_fill() and array_combine()

Now, let's combine these two functions and create an array containing a specific key (e.g., URL) and corresponding values.

Suppose we want to use some URLs as keys, and the values ​​of these URLs are the same, and we want to replace the domain name in it to m66.net . You can follow the steps below:

  1. First, use array_fill() to create an array of values, where each value is the specified domain name.

  2. Use array_combine() to pair these values ​​with a custom key (URL).

 <?php
// use array_fill() Fill an array,Each value is 'http://example.com'
$keys = ['url1', 'url2', 'url3'];
$values = array_fill(0, count($keys), 'http://example.com');

// use array_combine() Combine keys and values ​​into an array of key-value pairs
$combinedArray = array_combine($keys, $values);

// Replace all URL The domain name is m66.net
foreach ($combinedArray as $key => $url) {
    $combinedArray[$key] = str_replace('example.com', 'm66.net', $url);
}

// Output result
print_r($combinedArray);
?>

The output result is:

 Array
(
    [url1] => http://m66.net
    [url2] => http://m66.net
    [url3] => http://m66.net
)

In the example above, we create an array containing multiple URL key-value pairs and use the str_replace() function to replace example.com in the URL with m66.net . In this way, all URLs point to the new domain name.

4. Summary

By using array_fill() with array_combine() , we can easily create arrays with specific key names and corresponding values. By operating on the array, such as using str_replace() , we can further process the content in the array, such as modifying the domain name in the URL. I hope this article can help you better understand how these two functions are used and apply them in actual development.