Current Location: Home> Latest Articles> Replace array_fill_keys with array_combine

Replace array_fill_keys with array_combine

M66 2025-05-14

Under what circumstances can array_combine be used instead of array_fill_keys ?

There are many useful array functions in PHP, array_combine and array_fill_keys are functions used to manipulate arrays. Although their functions seem similar, their usage scenarios and usages are different in actual development. This article will explore under what circumstances can array_combine be used instead of array_fill_keys and their differences.

1. Basic concepts of array_combine and array_fill_keys

array_combine

The function of the array_combine function is to combine two arrays into an associative array. The first array is used as a key and the second array is used as the corresponding value. If the number of elements of the two arrays is not equal, array_combine returns false .

grammar :

 array_combine(array $keys, array $values): array|false

Example :

 $keys = ['a', 'b', 'c'];
$values = [1, 2, 3];
$result = array_combine($keys, $values);
print_r($result);

Output:

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

array_fill_keys

The array_fill_keys function is used to fill the specified value based on the given array of key names. It generates a new array where all key names correspond to the same values.

grammar :

 array_fill_keys(array $keys, mixed $value): array

Example :

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

Output:

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

2. The difference between array_combine and array_fill_keys

Although array_combine and array_fill_keys are both used to create associative arrays, their main difference is how the keys and values ​​of the array are handled:

  • array_combine requires two arrays, one for keys and one for values.

  • array_fill_keys only requires an array of keys and a value, which will fill each key with the same value.

3. Under what circumstances can array_combine be used instead of array_fill_keys ?

You can use array_combine instead of array_fill_keys in the following cases:

  • When all elements of the value array are the same : If you want to assign the same value to each key, and your value is fixed, you can create an array where all elements are filled with the same value. Then, use array_combine to combine this array of values ​​with the array of keys.

    Example :

     $keys = ['a', 'b', 'c'];
    $value = 100;
    $values = array_fill(0, count($keys), $value); // Create an array of the same values
    $result = array_combine($keys, $values);
    print_r($result);
    

    Output:

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

    In the above code, we first use array_fill to create an array with a value of 100 , and then merge keys and values ​​into an associative array through array_combine . This allows the same effect as array_fill_keys .

  • When the value array is dynamic : if the contents of the value array are not necessarily the same fixed value, using array_combine will be more flexible. You can dynamically generate different arrays of values ​​and then merge them with the array of keys.

    Example :

     $keys = ['a', 'b', 'c'];
    $values = [1, 2, 3];  // Dynamically generated array of values
    $result = array_combine($keys, $values);
    print_r($result);
    

    Output:

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

4. Summary

  • Use array_combine to combine an array of keys with a number of values, which is very suitable for dynamically generated arrays of values.

  • array_fill_keys is a more concise and intuitive choice when the value is fixed and you want to assign the same value to each key.

  • array_combine is suitable for scenarios where different values ​​need to correspond to keys one by one, while array_fill_keys is more suitable for all keys to use the same value.

Choosing the right function according to specific needs can make your code more concise and efficient.