PHP array_pad()函数与示例

素颜马尾好姑娘i 2023-03-05 12:55 43阅读 0赞

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:

句法:

  1. 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:

例子:

  1. Input:
  2. $arr = array(10, 20, 30);
  3. Function call:
  4. array_pad($arr, 5, 100);
  5. Output:
  6. Array
  7. (
  8. [0] => 10
  9. [1] => 20
  10. [2] => 30
  11. [3] => 100
  12. [4] => 100
  13. )

PHP code:

PHP代码:

  1. <?php
  2. $arr = array(10, 20, 30);
  3. //padding to 5 elements with value 100
  4. $result = array_pad($arr, 5, 100);
  5. //printing
  6. print_r ($result);
  7. $arr = array("Hello", "Guys");
  8. //padding to 5 elements with value "Bye!"
  9. $result = array_pad($arr, 5, "Bye!");
  10. //printing
  11. print_r ($result);
  12. ?>

Output

输出量

  1. Array
  2. (
  3. [0] => 10
  4. [1] => 20
  5. [2] => 30
  6. [3] => 100
  7. [4] => 100
  8. )
  9. Array
  10. (
  11. [0] => Hello
  12. [1] => Guys
  13. [2] => Bye!
  14. [3] => Bye!
  15. [4] => Bye!
  16. )

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

发表评论

表情:
评论列表 (有 0 条评论,43人围观)

还没有评论,来说两句吧...

相关阅读