Current Location: Home> Latest Articles> What are the similarities and differences between array_fill_keys and Laravel's Arr::fillKeys?

What are the similarities and differences between array_fill_keys and Laravel's Arr::fillKeys?

M66 2025-06-06

In PHP, array_fill_keys and Laravel's Arr::fillKeys are both used to fill arrays, but they belong to different implementations in the PHP native and Laravel frameworks, respectively. Although they are similar in functionality, there are some differences in usage, performance and some details. This article will discuss in detail the similarities and differences between these two methods, as well as their application scenarios and performance comparisons in actual development.

1. array_fill_keys function

array_fill_keys is a native function in PHP that creates a new array whose keys come from the specified array, and the corresponding value of each key is the same default value.

Usage example:

 <?php
$array = ['a', 'b', 'c'];
$filled = array_fill_keys($array, 1);
print_r($filled);
?>

Output result:

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

In this example, array_fill_keys takes each value in $array as a key and fills the same value of 1 for those keys.

Parameter description:

  • The first parameter is an array, which will be used as the key to the new array.

  • The second parameter is the value corresponding to each key.

2. Arr::fillKeys method

In the Laravel framework, Arr::fillKeys is a method in the Illuminate\Support\Arr class. Similar to array_fill_keys , it also fills the given key with a specified value, but it is optimized for some details and is usually used in conjunction with other features of Laravel.

Usage example:

 <?php
use Illuminate\Support\Arr;

$array = ['a', 'b', 'c'];
$filled = Arr::fillKeys($array, 1);
print_r($filled);
?>

Output result:

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

Parameter description:

  • The first parameter is an array that is used as the key to the new array.

  • The second parameter is the value corresponding to each key.

3. Comparison of similarities and differences

1. Functional similarity

array_fill_keys and Arr::fillKeys are very similar in functionality. They are all used to take each value in an array as the keys for the new array and assign the specified values ​​to these keys. The usage and results of both are almost the same, and are suitable for scenarios where arrays need to be filled.

2. Implementation method

  • array_fill_keys is a built-in function of PHP, belongs to the PHP core library and does not rely on any framework. It is suitable for pure PHP projects.

  • Arr::fillKeys is an auxiliary method provided by Laravel and is part of the Laravel framework. If you are developing a Laravel project, you can use Arr::fillKeys directly, which may have some performance optimizations and convenience features inside the framework.

3. Performance differences

In terms of performance, the difference between the two is usually not large, especially on small-scale data. However, if the application relies on Laravel's Arr toolset, using Arr::fillKeys will be more in line with Laravel's coding style and may be more advantageous when working in conjunction with other Laravel features. For large-scale data operations, Laravel will likely optimize performance at the framework level, especially when it is combined with database queries, caching and other operations.

Under certain extreme performance requirements, the PHP native function array_fill_keys may be slightly faster because it does not rely on additional framework layers. However, this performance difference usually does not significantly affect in actual development.

4. Applicable scenarios

  • array_fill_keys is suitable for any PHP project, especially those that do not rely on the Laravel framework.

  • Arr::fillKeys is suitable for Laravel projects, especially when used with tools such as Laravel collections, array helper functions, and more, allowing for a better integration experience.

4. Summary

  1. Similarity : array_fill_keys and Arr::fillKeys have almost the same functions, both used to fill arrays, but belong to the PHP core and Laravel framework respectively.

  2. Usage scenarios : array_fill_keys is suitable for pure PHP projects, while Arr::fillKeys is more suitable for use in Laravel frameworks.

  3. Performance Differences : Performance Differences are not noticeable in most applications, but array_fill_keys may be more suitable if extreme performance optimization is required.

Whichever you choose, the key is your project requirements and whether you are already using the Laravel framework. If your project is already using Laravel, it is recommended to use Arr::fillKeys because it is more in line with Laravel's code style and may integrate better with other features. If you are developing a pure PHP project, array_fill_keys will be a more suitable choice.