PHP array_pad() Function

Example

Returns 5 elements and inserts the value "blue" into the new elements of the array:

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

Run Instance

Definition and Usage

The array_pad() function inserts a specified number of elements with a specified value into the array.

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

Note:If size If the parameter is less than the length of the original array, the function will not delete any elements.

Syntax

array_pad(array,size,value)
Parameters Description
array Required. Specifies the array.
size Required. Specifies the number of elements in the array returned by the function.
value Required. Specifies the value of the new element returned by the function.

Technical Details

Return Value: Returns an array with new elements.
PHP Version: 4+

More Examples

Example 1

Use negative size parameter:

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

Run Instance