Skip to content

Difference Between Array_fill() And Array_fill_keys() Functions In PHP

In this article, you are going to learn the difference between array_fill() and array_fill_keys() functions in php.

The function array_fill() is used to assign or fill mixed values to an array by declaring a suitable number of elements in the array.

Whereas the function array_fill_keys() is used to fill mixed values to an array corresponding to the key values.

We have given the description of both the above function in detail in the below section. Join w3training School php training program and go through all the array used in php programming.

ARRAY_FILL() FUNCTION

Syntax : array_fill ($index_key, $number, $value);

Three parameters are used in this function which are described below.

  • $index_key- It represents the first value of the index in an array. If the index_key is negative say -3, the first index key will be -3 and the next index key will be zero and so on.
  • $number- It represents an integer value corresponding to the number of elements to be inserted. It should always be greater than zero or equal to zero. Negative values should be avoided.
  • $value- It represents the value to be inserted.

Result/ Return Value : On the successful execution of the function, an array filled with values will be returned.

Errors : If you have put a negative number inside the $number parameter, an E_WARNING will be generated.

E_WARNING

Example : An example of array_fill() function is shown below. 

php filloutput-1024x468

Output : You will get the following output after the successful execution of the php script. Here, we have taken 2 as the index key, 5 as the number of elements to be inserted and ‘blue’ as the value to be inserted or filled in the array. Clearly, in the output, the first element will have the index key equal to the declared value of the same i.e, 2. The number of elements will be 5 and the value assigned to each key is ‘blue’ as shown in the image below.

ARRAY_FILL_KEYS() FUNCTION

Syntax : array_fill_keys($keys, $value);

Two parameters are used in this function which are described below.

  • $keys- It represents an array of keys.
  • $value- It represents the value to be inserted.

Result/ Return Value : The successful execution of the function will return an array filled with the value.

php-fillkey-1024x447

Example : We have shown below an example of array_fill_keys() function. 

php-fillkeyoutput-1024x276

Output : The following output will be generated after successful execution of the above php script. Here, we have taken five keys ‘a’, ‘b’, ‘c’, ‘d’ and ‘g’ stored in the variable $key. We have taken another variable $a1 which stores the value ‘blue’ assigned to the array of keys. 

We have given a brief description about the difference between array_fill() and array_fill_keys() functions in php. Hope you liked this article. For further updates and related information, stay connected to our php blogs.

Facebook
Twitter
LinkedIn
Pinterest