array_fill_keys(): Fill array with values and keys

$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 )
  )

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer