Home > Backend Development > PHP Tutorial > 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

黄舟
Release: 2023-03-17 07:38:02
Original
1845 people have browsed it

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);
?>
Copy after login

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);
Copy after login
ParametersDescription
keys Required. Array whose values ​​will be used to populate the array keys.
valueRequired. 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);
?>
Copy after login

The running results are as follows :

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


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template