Home  >  Article  >  Backend Development  >  The function array_fill_keys() that fills the array with the key value of the specified key name given by php

The function array_fill_keys() that fills the array with the key value of the specified key name given by php

黄舟
黄舟Original
2017-11-07 10:44:131815browse

Example

Fill the array with the key value of the given specified key name:

<?php
$keys=array("a","b","c","d");
$a1=array_fill_keys($keys,"blue");
print_r($a1);
?>

Definition and usage

array_fill_keys() Function Fills the array with the key value of the given specified key name.

Syntax

array_fill_keys(keys,value);
Parameters Description
keys Required. Array whose values ​​will be used to populate the array keys.
value Required. Specifies the key values ​​used to populate the array.

Technical Details

Return value: Returns the populated array.
PHP version: 5.2+

Example:

<?php
$keys = array(&#39;foo&#39;, 5, 10, &#39;bar&#39;);
$a = array_fill_keys($keys, &#39;banana&#39;);
print_r($a);
?>

The running results are as follows :

Array(
    [foo] => banana
    [5] => banana
    [10] => banana
    [bar] => banana
)


The above is the detailed content of The function array_fill_keys() that fills the array with the key value of the specified key name given by php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn