PHP array_pad()函数与示例

PHP array_pad()函数 (PHP array_pad() function)

array_pad() function is used to pad an array to given size with a specified value and returns a new array with a specified value.

array_pad()函数用于将数组填充到具有指定值的给定大小,并返回具有指定值的新数组。

Syntax:

句法:

    array_pad(array, size, value);

Here,

这里,

  • array is an array in which we have to add the elements.

    数组是我们必须在其中添加元素的数组。

  • size is the length/size of the array.

    size是数组的长度/大小。

  • value is the value to be added to pad an array.

    value是要填充数组的值。

Examples:

例子:

    Input:
    $arr = array(10, 20, 30);

    Function call:
    array_pad($arr, 5, 100);

    Output:
    Array
    (    
        [0] => 10   
        [1] => 20   
        [2] => 30   
        [3] => 100  
        [4] => 100  
    )

PHP code:

PHP代码:

<?php
    $arr = array(10, 20, 30);
    //padding to 5 elements with value 100
    $result = array_pad($arr, 5, 100);
    //printing
    print_r ($result);

    $arr = array("Hello", "Guys");
    //padding to 5 elements with value "Bye!"
    $result = array_pad($arr, 5, "Bye!");
    //printing
    print_r ($result);    
?>

Output

输出量

Array
(    
    [0] => 10   
    [1] => 20   
    [2] => 30   
    [3] => 100  
    [4] => 100  
)    
Array
(    
    [0] => Hello
    [1] => Guys 
    [2] => Bye! 
    [3] => Bye! 
    [4] => Bye! 
)


翻译自: https://www.includehelp.com/php/array_pad-function-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值