Current Location: Home> Latest Articles> The actual application of array_fill() + array_map()

The actual application of array_fill() + array_map()

M66 2025-06-05

In PHP, processing arrays is a very common task. Whether it is generating array data or operating on data in an array, array functions are essential tools. The two functions array_fill() and array_map() are very efficient in batch generation and processing arrays. This article will explore how to combine these two functions to efficiently generate and process array data and improve development efficiency.

1. Introduction and application of array_fill()

array_fill() is an array function provided by PHP. Its function is to generate an array filled with the specified value. We can create a new array by specifying the starting index, length and padding value of the array.

The basic syntax of array_fill() :

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

  • $num : The length of the array.

  • $value : The value to fill the array.

For example, if we want to generate an array containing 10 elements, all elements in the array have values ​​of 0 , you can use array_fill() like this:

 $array = array_fill(0, 10, 0);
print_r($array);

The output result is:

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

As shown above, array_fill() will generate an array of length 10 and each element has a value of 0 .

2. Introduction and application of array_map()

array_map() is another array handler in PHP that applies a callback function to each element in an array and returns a new array. array_map() can be very convenient to batch convert array elements, especially when the data needs to be processed uniformly.

The basic syntax of array_map() :

 array_map(callable $callback, array $array1, array $array2, ...): array
  • $callback : A callback function that processes each element in the array.

  • $array1, $array2, ... : array to be processed. array_map() will execute a callback function on each element of these arrays in turn.

For example, if we have an array that wants to multiply each element of it by 2, we can use array_map() like this:

 $array = [1, 2, 3, 4, 5];
$result = array_map(function($item) {
    return $item * 2;
}, $array);

print_r($result);

The output result is:

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

With array_map() we successfully multiply each element in the array by 2.

3. Use array_fill() and array_map() in combination

The combination of array_fill() and array_map() can provide a very efficient solution when batch generation and processing array data. The following is a practical application scenario. Suppose we want to generate an array containing multiple user information, and each user's information needs to be processed through certain logic.

Example: Generate user ID and add domain name for each user

Suppose we need to generate an array with 10 user IDs, and then generate a URL with a domain name for each user ID via array_map() . First, we use array_fill() to generate an initial array, and then use array_map() to process each user ID.

 // Generate a containing 10 Array of elements,The value of each element is 1000
$user_ids = array_fill(0, 10, 1000);

// pass array_map For each userIDGenerate a containing域名的URL
$urls = array_map(function($user_id) {
    return "https://m66.net/user/{$user_id}";
}, $user_ids);

// Output result
print_r($urls);

The output result is:

 Array
(
    [0] => https://m66.net/user/1000
    [1] => https://m66.net/user/1000
    [2] => https://m66.net/user/1000
    [3] => https://m66.net/user/1000
    [4] => https://m66.net/user/1000
    [5] => https://m66.net/user/1000
    [6] => https://m66.net/user/1000
    [7] => https://m66.net/user/1000
    [8] => https://m66.net/user/1000
    [9] => https://m66.net/user/1000
)

In this example, we first use array_fill() to generate an array containing 10 elements, each element has a value of 1000 , and then use array_map() to generate a URL with a domain name for each user ID. In this way, we are able to batch generate and process arrays.

4. Summary

array_fill() and array_map() are very useful functions in PHP, especially when batch generation and processing array data. By combining these two functions, we can efficiently create and modify arrays, simplify code and improve development efficiency. Mastering the application of these array functions will greatly improve your ability in PHP development.