Current Location: Home> Latest Articles> Initialize key values ​​in batches using array_fill() and array_keys()

Initialize key values ​​in batches using array_fill() and array_keys()

M66 2025-06-05

How to batch initialize the key value of an array using array_fill() and array_keys() and set the same value for each key

In PHP, we can use the two functions array_fill() and array_keys() to batch initialize the key values ​​of the array and set the same value for each key. Through the combination of these two, the efficiency and readability of the code can be improved when processing large amounts of data.

1. array_keys() function

The array_keys() function returns an array of all key names in an array and can be used to extract all keys in an array. The syntax of this function is as follows:

 array_keys(array $array, mixed $value = null, bool $strict = false) : array
  • $array : original array.

  • $value : The value to be found, the default is null .

  • $strict : Whether to compare strictly, default is false , indicating that data types are not distinguished.

2. array_fill() function

The array_fill() function is used to fill an array, specifying that the fill starts from the specified position, and assign the same value to all elements. Its syntax is as follows:

 array_fill(int $start_index, int $num, mixed $value) : array
  • $start_index : The index to start.

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

  • $value : The value to be filled.

3. Batch initialization of arrays using array_keys() and array_fill()

Suppose we have an array with some data, we want to set all key names of that array to a new array and set the same value for each key. Here is a sample code showing how to achieve this.

 <?php
// Original array
$originalArray = [
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'cherry',
];

// Get all key names of the array
$keys = array_keys($originalArray);

// use array_fill() Create a new array,Set all keys to the same value
$filledArray = array_fill(0, count($keys), 'm66.net');

// use array_combine() Combine key names and padded values ​​into a new associative array
$newArray = array_combine($keys, $filledArray);

// Output result
print_r($newArray);
?>

Code explanation:

  1. array_keys($originalArray) extracts all key names in the original array $originalArray and saves them in the $keys array.

  2. array_fill(0, count($keys), 'm66.net') starts with index 0, creates a new array of the same length as the $keys array and sets the value 'm66.net' for each element.

  3. array_combine($keys, $filledArray) merges $keys and $filledArray into a new associative array $newArray , the key name comes from $keys , and the corresponding value of each key is 'm66.net' .

4. Output example

After executing the above code, the output will be:

 Array
(
    [a] => m66.net
    [b] => m66.net
    [c] => m66.net
)

5. Practical application scenarios

This approach is very suitable for scenarios where array data needs to be initialized in batches, especially when working with configuration items or initializing form data. For example, suppose you have a website that needs to initialize multiple settings, each setting should have the same default value. You can quickly achieve this requirement in this way.

Hopefully this article helps you understand how to batch initialize the key values ​​of an array using array_fill() and array_keys() and set the same value for each key. If you have any questions, feel free to ask!