Current Location: Home> Latest Articles> How to use array_fill() to quickly initialize cached data structures?

How to use array_fill() to quickly initialize cached data structures?

M66 2025-05-14

In high concurrency PHP projects, the initialization efficiency of cached data structures often directly affects the system's response speed and resource utilization. Especially when dealing with arrays with large number of predefined indexes, if you assign values ​​manually, not only will the code be lengthy, but it will also be prone to errors. At this time, the array_fill() function is a very practical tool.

What is array_fill() ?

array_fill() is a built-in function in PHP that generates an array of specified length and fills all elements with the same value. Its syntax is as follows:

 array_fill(int $start_index, int $count, mixed $value): array
  • $start_index : The starting index of the array (can be a negative number)

  • $count : The number of generated elements

  • $value : The default value of all elements

Example: Initialize the cache structure

Suppose you want to prepare an initial cache data structure for 100 users in a system, and the initial state of each user is ['logged_in' => false, 'last_activity' => null] . It will be very verbose to write in traditional ways, and using array_fill() can greatly simplify.

 $defaultCache = ['logged_in' => false, 'last_activity' => null];
$userCache = array_fill(0, 100, $defaultCache);

print_r($userCache);

In this example, we quickly generated cache items for 100 users using the same default structure.

More complex structure: dynamic generation combined with user ID

In real business, you may need to dynamically generate cache arrays in combination with user IDs. At this time, you can use it with array_fill_keys() :

 $userIds = range(101, 200);  // Simulate usersIDList
$defaultCache = ['logged_in' => false, 'last_activity' => null];

$userCache = array_fill_keys($userIds, $defaultCache);

The generated $userCache will be keyed with the user ID and initialized into a unified cache structure.

Scenario expansion: Preset URL status cache

Suppose you are building a crawler system and intend to initialize the crawl state for a batch of URLs. It is very convenient to preset the status with array_fill() :

 $urls = [
    'https://m66.net/page1',
    'https://m66.net/page2',
    'https://m66.net/page3',
];

$defaultStatus = ['fetched' => false, 'status_code' => null];

$urlStatusCache = array_fill_keys($urls, $defaultStatus);

In this way, each URL has its own initial crawl state, and the code is both concise and maintainable.

Summarize

Using array_fill() (or array_fill_keys() ) can greatly improve the efficiency of initialization of data structures, especially in scenarios where unified structure and default values ​​are required:

  • Save code volume and reduce repetitive work

  • Easier to maintain and read

  • Avoid omissions or initialization errors

Mastering this type of array function is one of the keys to writing efficient PHP code!