PHP array pad

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP: array_pad() function

PHP: Pad array to the specified length with a value

The array_pad() function is used to padding (insert) a specified number of elements, with a specified value, into an array.
If the specified value is positive then the array is padded on the right, if it is negative then padded on the left.
If the specified value is less than or equal to the length of the input then no padding takes place.

Version

(PHP 4 and above)

Syntax

array array_pad (input_array, pad_size, pad_value)

Parameters

Name

Description

Required /
Optional

Type

input_array

Specifies the name of the array.

Required

Array

pad_size

A total number of elements in the resulting array after padding.

Required

Array

pad_value

Value with which padding will take place, must be less than the size of the input_array.

Required

Mixed*

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value

An array with new elements.

Value Type : Array

Pictorial Presentation :

php array array_pad() function

Example :

  1. $input_array= array(1,7,10,6);  

  2. $result=array_pad($input_array,5,330);  

  3. print_r($result);  

  4. ?>  

Output :

Array ( [0] => 1 [1] => 7 [2] => 10 [3] => 6 [4] => 330 )