Current Location: Home> Latest Articles> How to avoid problems caused by array_fill() filling value shared references?

How to avoid problems caused by array_fill() filling value shared references?

M66 2025-06-05

How to avoid the reference sharing issue of values ​​when filling arrays with array_fill()?

In PHP, the array_fill() function is usually used to quickly fill an array, which accepts three parameters: the starting index, the length of the array, and the value of the fill. When we fill an array with reference type data, we may encounter the problem of reference sharing. That is, if we modify an element in the array, it may affect the values ​​of other elements because they share the same reference.

Example of reference sharing issues

 <?php
// Define an array
$reference = [];
// use array_fill Fill the array
$size = 5;
$reference = array_fill(0, $size, []);

// Modify the first element of the array
$reference[0]['name'] = 'John';

// Print an array
print_r($reference);
?>

When you run the above code, you will find that all the filled elements are actually pointing to the same array reference, so after modifying $reference[0] , all other elements are also changed. This is the root cause of the quotation sharing problem.

Solution

To avoid the reference sharing problem when array_fill() fills an array, there are a few different techniques that can be used to ensure that each array element has an independent copy.

Method 1: Use array_map() to combine anonymous functions

A common solution is to use array_map() with anonymous functions to create a new array copy for each element. This avoids reference sharing issues.

 <?php
// Define the size of the array
$size = 5;
// use array_map Fill the array,and create a new array copy for each element
$reference = array_map(function() {
    return []; // Return a new empty array
}, array_fill(0, $size, null));

// Modify the first element of the array
$reference[0]['name'] = 'John';

// Print an array
print_r($reference);
?>

This method creates a new array for each array element via array_map() , thus avoiding reference sharing issues.

Method 2: Manually fill the array

Another way is to avoid reference sharing issues by manually populating the array. This method does not rely on array_fill() , but uses a foreach or for loop to fill the array one by one.

 <?php
// Define the size of the array
$size = 5;
// 手动Fill the array
$reference = [];
for ($i = 0; $i < $size; $i++) {
    $reference[] = []; // Add a new empty array every time
}

// Modify the first element of the array
$reference[0]['name'] = 'John';

// Print an array
print_r($reference);
?>

The advantage of this approach is that a new array instance is created every time without causing a reference sharing problem.

Summarize

In PHP, when using array_fill() to fill an array, if the filled value is a reference type, it may cause the same reference to be shared between the array elements, resulting in unexpected modification issues. To avoid this problem, we can use array_map() or manually fill the array to ensure that each element is a separate copy.

With these methods, you can use array padding more safely without worrying about the problems with reference sharing.