Current Location: Home> Latest Articles> The similarities and similarities between array_fill_keys and array_map

The similarities and similarities between array_fill_keys and array_map

M66 2025-05-14

title:
What is the difference and connection between array_fill_keys and array_map?

text:

In PHP, array_fill_keys and array_map are common functions for operating arrays, but their functions and uses are different. Today we will discuss the differences and connections between these two functions.

array_fill_keys function

The array_fill_keys function is used to fill an array according to the specified key and give a value. The function prototype is as follows:

 array_fill_keys(array $keys, mixed $value): array
  • Parameter description:

    • $keys : is an array containing keys. array_fill_keys will fill a new array based on these keys.

    • $value : is the value that needs to be assigned to each key.

  • Return value:

    • Returns a new array containing the specified key and value.

  • Sample code:

 $keys = ['a', 'b', 'c'];
$value = 1;
$result = array_fill_keys($keys, $value);
print_r($result);

Output:

 Array
(
    [a] => 1
    [b] => 1
    [c] => 1
)

As shown above, array_fill_keys creates an array containing the specified keys, and the value of each key is 1 .

array_map function

The array_map function applies a callback function to each element of the array and returns a new array. The function prototype is as follows:

 array_map(callable $callback, array $array, ...$arrays): array
  • Parameter description:

    • $callback : is a callback function that handles each element.

    • $array : is an array to be processed. Multiple arrays can be passed in, and the callback function will process the corresponding elements of each array in turn.

  • Return value:

    • Returns a new array containing each element processed by the callback function.

  • Sample code:

 $array = [1, 2, 3, 4];
$result = array_map(function($item) {
    return $item * 2;
}, $array);
print_r($result);

Output:

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

As shown above, array_map applies a callback function to each element in the array, multiplying its value by 2.

Differences and connections

1. Functional differences:

  • array_fill_keys is to create an array based on the specified key and assign the same value to each key. It does not operate on elements in the array, it just fills a new array.

  • array_map operates on each element in the array, uses a callback function to modify each element, and returns the modified array.

2. Differences in usage scenarios:

  • array_fill_keys is suitable for when you already have a set of keys and you need to fill in uniform values ​​for those keys.

  • array_map is often used to convert data in an array when you need to do some kind of processing on each element in an array.

3. Contact:

  • To some extent, array_map can be used to operate on each element in an array, thereby indirectly affecting the structure of the array, similar to modifying the content of the array through a callback function. array_fill_keys focuses more on how to use given keys and values ​​to build a new array. Both involve elements of the array when processing arrays, but their processing methods are different from application scenarios.

Summarize

  • array_fill_keys is used to fill an array according to the specified key and assign the same value.

  • array_map is used to apply a callback function to each element in the array, returning a processed new array.

They are both very useful when working with arrays, but their respective focus and usage scenarios are different. Choosing the right function can make the code more concise and efficient.

The above is a detailed explanation of the differences and connections between array_fill_keys and array_map functions. Hope it will help you understand these two functions!