PHP array_pad() Function

PHP array_pad Function


Description

The array_pad() function inserts a value into an array up to the specified length.

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

The basic syntax of the array_pad() function is given with:

     array_pad(arraysizevalue);

Parameters:

The array_pad() function accepts the following parameters.

Parameter Description
array Required. Specifies the array in which to insert the elements.
size Required. Specifies the new size of the array.
value Required. Specifies the value to insert if array's length is less than the size.

Example : 

If size is positive then the array is padded on the right, if it's negative then padded on the left.

Output :

Array
(
    [0] => online
    [1] => online
    [2] => Web
    [3] => Designing
    [4] => House
)

Example2 :If size is positive then the array is padded on the right

Output :

Array
(
    [0] => Web
    [1] => Designing
    [2] => House
    [3] => online
    [4] => online
)