PHP | array_fill_keys() Function
The array_fill_keys() function is a builtin function in PHP and is used to create a new array filled with the given keys and value provided as an array to the function.
Syntax:
array array_fill_keys ( $keys, $value )
Parameters: This function accepts two parameters, keys and their values to be present in the new array. Both of this parameters are described below:
- $keys: This parameter is an array consisting of keys that are to be used for creating the new array. If the $keys array contains any illegal value then it is converted into a string and used.
- $value: This parameter can be a single value or a list of values. This parameter represents the value of the keys that are to be inserted into the array. If this parameter is an array, then the new array created will be a 2-d array where each element of $keys array will be a key and every key in this new array will have $value array as a value.
Return value: This function returns an array consisting of key-value pairs that are provided to the function as parameters.
Examples:
Input : $keys = array('golden', 25, 560, 'age') array_fill_keys($keys, 'majestic') Output : Array ( [golden] => majestic [25] => majestic [560] => majestic [age] => majestic ) Input :$keys = array('tumult', '25', 560, 'cater') array_fill_keys($keys, 'limited') Output : Array ( [tumult] => limited [25] => limited [560] => limited [cater] => limited )
In both the example the keys to be used with the new array are provided as an array to the function and the value to be used is provided as the second argument.
Below programs illustrate the array_fill_keys() function in PHP:
Program 1:
<?php $keys = array ( 'golden' , 25, 560, 'age' ); // Creating new array with specified keys $a = array_fill_keys( $keys , 'majestic' ); print_r( $a ); ?> |
Output:
Array ( [golden] => majestic [25] => majestic [560] => majestic [age] => majestic )
Program 2:
<?php $keys = array ( 'tumult' , '25' , 560, 'cater' ); // Creating new array $a = array_fill_keys( $keys , 'limited' ); print_r( $a ); ?> |
Output:
Array ( [tumult] => limited [25] => limited [560] => limited [cater] => limited )
Program 3:
<?php $keys = array ( 'tumult' , '25' , 560, 'cater' ); $value = array (5,10); // Creating new array $a = array_fill_keys( $keys , $value ); print_r( $a ); ?> |
Output:
Array ( [tumult] => Array ( [0] => 5 [1] => 10 ) [25] => Array ( [0] => 5 [1] => 10 ) [560] => Array ( [0] => 5 [1] => 10 ) [cater] => Array ( [0] => 5 [1] => 10 ) )
Reference:
http://php.net/manual/en/function.array-fill-keys.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | key() Function
- p5.js | second() function
- PHP | each() Function
- p5.js | value() Function
- PHP | Ds\Map xor() Function
- PHP | dir() Function
- p5.js | max() function
- PHP | Ds\Map put() Function
- PHP | pos() Function
- PHP | Ds\Set contains() Function
- PHP | tan( ) Function
- PHP | Ds\Map first() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.