array_keys
PHP array_fill_keys()函数 (PHP array_fill_keys() Function)
array_fill_keys() function is used to fill a given array with a value where keys are specified. We will use an array that contains keys, all these keys will be filled with the value and return a new array.
array_fill_keys()函数用于在给定数组中填充指定键的值。 我们将使用包含键的数组,所有这些键都将填充值并返回一个新数组。
Syntax:
句法:
array_fill_keys(keys, value) : array
Here,
这里,
keys is an array and its value will be used as keys for the new array.
keys是一个数组,其值将用作新数组的键。
value to be filled for the keys.
密钥要填充的值 。
Examples:
例子:
Input:
$keys = array("Name", "Age", "Gender", "Address");
value = "N/A"
Function call:
array_fill_keys($keys, "N/A");
Output:
Array
(
[Name] => N/A
[Age] => N/A
[Gender] => N/A
[Address] => N/A
)
PHP code:
PHP代码:
<?php
//keys (creating array)
$keys = array("Name", "Age", "Gender", "Address");
//creating an array with these keys
//and, filling all keys with default value
$arr = array_fill_keys($keys, "N/A");
print_r ($arr);
?>
Output
输出量
Array
(
[Name] => N/A
[Age] => N/A
[Gender] => N/A
[Address] => N/A
)
翻译自: https://www.includehelp.com/php/array_fill_keys-function-with-example.aspx
array_keys