PHP - Function array_pad()


Advertisements


Syntax

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.

Parameters

Sr.No Parameter & Description
1

array

Required. Specifies an array.

2

pad_size

Required. Specifies the number of elements in the array returned from the function.

3

pad_value

Required. Specifies the value of the new elements in the array returned from the function.

Return Values

It returns the resulting array.

Example

Try out following example −

<?php
   $array1 = array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
   
   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 )

php_function_reference.htm

Advertisements