Latest web development tutorials

PHP array_pad () function

PHP Array Reference Complete PHP Array Reference

Examples

Back five elements, and "blue" values ​​into an array of new elements:

<?php
$a=array("red","green");
print_r(array_pad($a,5,"blue"));
?>

Running instance »

Definition and Usage

array_pad () function element with the specified value of a specified number of insert into the array.

Tip: If you set the size parameter is negative, the function will insert a new element before the original array (see example below).

Note: If thesize argument is less than the length of the original array, the function does not remove any elements.


grammar

array_pad( array,size,value )

参数 描述
array 必需。规定数组。
size 必需。规定从函数返回的数组元素个数。
value 必需。规定从函数返回的数组中新元素的值。

technical details

return value: It returns an array with the new elements.
PHP version: 4+


More examples

Example 1

Negative values ​​of size parameters:

<?php
$a=array("red","green");
print_r(array_pad($a,-5,"blue"));
?>

Running instance »


PHP Array Reference Complete PHP Array Reference