$new_array = array_fill_keys ($keys_array, $value);
$keys_array : An array whoes values will be used as keys.
$value: This will be used as filling ( the value of the output array )
Output will be a new array with filled keys and value.
Example with two arrays
$keys=array('One' ,'Two','Three','Four');
$input='First';
$result=array_fill_keys($keys,$input);
while (list ($key, $val) = each ($result)) {
echo "$key -> $val <br>";
}
Output is here.
One -> First
Two -> First
Three -> First
Four -> First
Example 2
$keys=array('One' ,2,'3','F');
$input='First';
$result=array_fill_keys($keys,$input);
while (list ($key, $val) = each ($result)) {
echo "$key -> $val <br>";
}
Output
One -> First
2 -> First
3 -> First
F -> First
Example 3 : Using associative array
$keys=array('One' ,'Two','Three');
$input=array('First','Second');
$result=array_fill_keys($keys,$input);
print_r($result);
Output
Array (
[One] => Array ( [0] => First [1] => Second )
[Two] => Array ( [0] => First [1] => Second )
[Three] => Array ( [0] => First [1] => Second )
)
Returns the First Key →
Creating array →
← Array REFERENCE
← Subscribe to our YouTube Channel here