array_keys_PHP array_fill_keys()函数与示例

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值