PHP array_fill_keys() Function

PHP array_fill_keys() Function


Hello folks! welcome back to a new section of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP array_fill_keys() Function.

The PHP array_fill_keys() function fills an array with the value of the value parameter, using the values of the keys array as keys.

Syntax

Following below is the syntax to use this function -

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


Parameter Details

Sr.NoParameter & Description
1

keys

It is an array of values that will be used as keys

2

value

It may an string or an array of values


Return Value

It returns the filled array.

Example

Try out the below example -

<?php
   $ = array('f1', 5, 10, 'b1');
   $a = array_fill_keys($input, 'banana');
   
   print_r($a)
?> 

Output

When the above code is executed, it will produce the following result -

Array (
   [f1] => banana
   [5] => banana
   [10] => banana
   [b1] => banana
)


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP array_filter() Function.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.