PHP array_pad() Function

0

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.

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

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 )