HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

PHP array_pad() Function

PHP Array Reference PHP Array Reference

Example

Return 5 elements and insert a value of "blue" to the new elements in the array:

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

Run example »

Definition and Usage

The array_pad() function inserts a specified number of elements, with a specified value, to an array.

Tip: If you assign a negative size parameter, the function will insert new elements BEFORE the original elements (See example below).

Note: This function will not delete any elements if the size parameter is less than the size of the original array.


Syntax

array_pad(array,size,value)

Parameter Description
array Required. Specifies an array
size Required. Specifies the number of elements in the array returned from the function
value Required. Specifies the value of the new elements in the array returned from the function

Technical Details

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


More Examples

Example 1

Using a negative size parameter:

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

Run example »


PHP Array Reference PHP Array Reference

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE