Sunday 25 August 2013

PHP Function array_pad()

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.

Paramters

ParameterDescription
arrayRequired.Specifies an array.
pad_sizeRequired.Specifies the number of elements in the array returned from the function.
pad_valueRequired.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 following result:
Array 
( 
   [a] => Horse 
   [b] => Cat 
   [c] => Dog 
   [0] => COW 
   [1] => COW 
   [2] => COW 
   [3] => COW 
)

No comments:

Post a Comment