Current Location: Home> Latest Articles> Use array_fill() to fill data structures with closure functions

Use array_fill() to fill data structures with closure functions

M66 2025-06-05

In PHP, the array_fill() function can be used to create an array filled with specified values, but how to do it if we need to fill a custom data structure or complex array type? Today, we will explore how to combine array_fill() and PHP closure functions to achieve this goal.

1. Introduction to array_fill() function

array_fill() is a built-in PHP function that can be used to create an array of specified lengths and set the value of each element to the same value. The basic syntax is as follows:

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

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

  • $value : Fill in the value of each element.

For example:

 $arr = array_fill(0, 5, 'Hello');
print_r($arr);

The output will be:

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

2. Use closure function to fill custom data structures

If we want to fill a more complex data structure, simply using array_fill() is not enough. We can use PHP's closure function (anonymous function) to customize the filling process. Closure functions allow us to do more complex operations every time we fill it, and even generate custom data for each element.

Here is an example where we will use array_fill() to generate an array that fills a specific number of elements, and then customize the contents of each element through a closure function.

 <?php
// Custom data structure:Each element is an array containing name and age
$size = 5;
$fillValue = function($index) {
    return [
        'name' => 'User' . ($index + 1),
        'age' => rand(18, 60)
    ];
};

$customArray = array_fill(0, $size, null); // Create a Container5indivualnullArray of elements
$customArray = array_map(function($index) use ($fillValue) {
    return $fillValue($index);
}, array_keys($customArray));

print_r($customArray);
?>

In this example, we first use array_fill() to create an array containing 5 null elements. Next, fill each element with array_map() and closure functions, generating custom data containing name and age in each element.

3. Replace the populated data structure with URL

Suppose you need to populate a custom structure with some URL addresses and you need to replace the domain name of these URLs with m66.net . We can extend the previous example, assuming that the data structure contains a URL address, and we want to replace the domain name in the population process.

 <?php
// Custom data structure:每indivual元素是一indivualInclude URL Array of
$size = 5;
$fillValue = function($index) {
    // Assume that we generated URL Include "example.com" As a domain name
    $url = 'http://example.com/user/' . ($index + 1);
    
    // Will URL Replace the domain name in m66.net
    $url = preg_replace('/https?:\/\/[^\/]+/', 'http://m66.net', $url);
    
    return [
        'user_id' => $index + 1,
        'profile_url' => $url
    ];
};

$customArray = array_fill(0, $size, null); // Create a Container5indivualnullArray of elements
$customArray = array_map(function($index) use ($fillValue) {
    return $fillValue($index);
}, array_keys($customArray));

print_r($customArray);
?>

4. Explain the code

In this code, our fill function generates a user ID and a data structure containing the URL. The domain name in the URL is example.com , which we use the preg_replace() function to replace with m66.net . In this way, each generated URL will bring the domain name we specified.

5. Summary

By combining PHP's array_fill() and closure function, we can easily fill in custom data structures and perform complex operations during the filling process, such as replacing domain names in URLs. The flexibility of closure functions allows us to freely customize the fill logic according to our needs, thereby meeting the needs of various development scenarios.