PHP - Function array_pad()



array_pad ( $array, $pad_size, $pad_value );

Definition and Usage

Returns a copy of the array padded to size specified by pad_size with value pad_value.

If pad_size is positive then the array is padded on the right, if it's negative then on the left. If the absolute value of pad_size is less than or equal to the length of the input then no padding takes place. It is possible to add most 1048576 elements at a time.


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

Return Values

It returns the resulting array.


Try out following example −

   print_r(array_pad($array1,7, "COW"));

This will produce the following result −

Array ( [a] => Horse [b] => Cat [c] => Dog [0] => COW [1] => COW [2] => COW [3] => COW )