PHP array_pad() Function

The array_pad() function is used to padding (insert) a specified number of elements, with a specified value, into 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.

ParameterDescription
arrayRequired. Specifies an array
sizeRequired. Specifies the number of elements in the array returned from the function
valueRequired. Specifies the value of the new elements in the array returned from the function

Syntax

 array_pad(array,size,value) 

Example

$array1 = array("Cooper","Samuel");
print_r(array_pad($array1,-6,"James"));

Output

Array ( [0] => James [1] => James [2] => James [3] => James [4] => Cooper [5] => Samuel ) 

Leave a Reply

Your email address will not be published. Required fields are marked *