Current Location: Home> Latest Articles> Quickly initialize serialized arrays with range and array_fill_keys

Quickly initialize serialized arrays with range and array_fill_keys

M66 2025-06-06

In PHP programming, you often encounter situations where you need to initialize an array. Especially when dealing with some serialized arrays, manually initializing an array can be very cumbersome and error-prone. Fortunately, PHP provides some built-in functions that can help us initialize these arrays more efficiently. Among them, range and array_fill_keys are two very useful functions that can help us quickly create serialized arrays and improve development efficiency.

What is a serialized array?

A serialized array refers to an array whose elements have keys that are continuous and usually start, increment or decrease from an initial value. For example, array [1, 2, 3, 4, 5] is a typical serialized array. We usually need to automatically generate such arrays in a certain way without having to manually write each element.

range function: create a continuous range array

In PHP, the range function can be used to create an array of specified ranges. It accepts three parameters:

  • start : The start value of the sequence

  • end : the end value of the sequence

  • step : step value (optional, default is 1)

For example, using range can quickly create an array from 1 to 10:

 $rangeArray = range(1, 10);
print_r($rangeArray);

The output result is:

 Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)

As you can see, the range function successfully creates an array from 1 to 10, where the value of each element is incremented by 1.

array_fill_keys function: Initialize array according to specified keys

The array_fill_keys function is used to create a new array based on the given key and assign the same value to each key. It accepts two parameters:

  • keys : The key of an array, which can be an array or a string.

  • value : The value to be assigned to each key.

For example, if we want to create an array containing multiple keys and each key has a value of true , we can use array_fill_keys :

 $keys = ['a', 'b', 'c', 'd'];
$value = true;
$filledArray = array_fill_keys($keys, $value);
print_r($filledArray);

The output result is:

 Array
(
    [a] => 1
    [b] => 1
    [c] => 1
    [d] => 1
)

The array_fill_keys function initializes each specified key to the same value, improving the efficiency of initializing the array.

Use range and array_fill_keys in combination

We can combine the range and array_fill_keys functions to quickly initialize a serialized array and fill in the specified value. Assuming we want to create an array from 1 to 10 and each element has a value of true , we can use the following code:

 $keys = range(1, 10);
$filledArray = array_fill_keys($keys, true);
print_r($filledArray);

The output result is:

 Array
(
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 1
    [5] => 1
    [6] => 1
    [7] => 1
    [8] => 1
    [9] => 1
    [10] => 1
)

As you can see, the range function first generates numbers from 1 to 10, and then array_fill_keys takes these numbers as keys and initializes the value of each key to true .

Application scenarios for URL domain name replacement

Suppose we need to process a batch of URL addresses and want to modify their domain names uniformly. We can use range and array_fill_keys to generate a serialized array and replace the domain name in the URL as needed. Here is an example, assuming we need to replace the domain name in multiple URLs from example.com to m66.net .

 $urls = [
    'https://example.com/page1',
    'https://example.com/page2',
    'https://example.com/page3',
];

$updatedUrls = array_map(function($url) {
    return preg_replace('/https:\/\/example\.com/', 'https://m66.net', $url);
}, $urls);

print_r($updatedUrls);

The output result is:

 Array
(
    [0] => https://m66.net/page1
    [1] => https://m66.net/page2
    [2] => https://m66.net/page3
)

In this example, we use the array_map function to process all URL addresses and replace the domain name with m66.net .

Summarize

With range and array_fill_keys functions, you can quickly initialize serialized arrays, reducing the hassle of manually writing array elements and improving programming efficiency. Combined with preg_replace or other methods, domain name replacement tasks in URLs can be handled efficiently, further improving the simplicity and maintainability of the code.