Current Location: Home> Latest Articles> Use array_keys to get the key and reset the array value with array_fill_keys

Use array_keys to get the key and reset the array value with array_fill_keys

M66 2025-06-06

In PHP, array_keys and array_fill_keys are two commonly used array functions. array_keys can help us get all keys in the array, while array_fill_keys can allow us to fill the array with the same value using the specified key. These two functions can be used in combination to achieve very practical operations.

What is the array_keys function?

The array_keys function returns all key names in the array. Its syntax is as follows:

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

  • $value (optional): If this parameter is provided, array_keys returns all keys with a value of $value .

  • $strict (optional): If set to true , perform strict comparison.

What is the array_fill_keys function?

The array_fill_keys function can fill a new array based on the provided key name array, and all values ​​are set to the specified value. Its syntax is as follows:

 array_fill_keys(array $keys, mixed $value): array
  • $keys : an array containing key names.

  • $value : The value used to fill.

How to reset the value of an array using array_keys and array_fill_keys in combination?

By combining these two functions, we can first get all key names in the array through array_keys , and then use array_fill_keys to reset the values ​​of these keys.

Sample code

Suppose we have an array containing multiple key-value pairs, and we want to reset the values ​​of all keys in this array to a specified value, such as 0 .

 <?php

// Original array
$array = [
    'apple' => 10,
    'banana' => 20,
    'cherry' => 30,
];

// use array_keys Get all key names
$keys = array_keys($array);

// use array_fill_keys Reset the value
$newArray = array_fill_keys($keys, 0);

// Output reset array
print_r($newArray);

?>

Code parsing

  1. array_keys($array) : This function returns all key names in the array $array . The result is an array containing 'apple' , 'banana' and 'cherry' .

  2. array_fill_keys($keys, 0) : Use the array_fill_keys function to create a new array with the same key name as the $keys array, but all values ​​are filled with 0 .

Output result:

 Array
(
    [apple] => 0
    [banana] => 0
    [cherry] => 0
)

As shown above, the values ​​of all keys have been successfully reset to 0 .

Practical application scenarios

This technique is very useful when dealing with big data, especially when it is necessary to reinitialize the values ​​of the array. Suppose you get an array of various data from a database, and now you want to reset or initialize some data, this method can effectively simplify the code.

Things to note

  • When using array_fill_keys , make sure the passed array of key names is valid. If the keyname array is empty, array_fill_keys will return an empty array.

  • If some key names in the original array are deleted, or the value you want to reset is different from the original array's value, array_fill_keys can easily overwrite the original value.