w3resource

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

Example:

<?php
$input_array= array(1,7,10,6);
$result=array_pad($input_array,5,330);
print_r($result);
?>

Output:

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

Pictorial Presentation:

php function reference: array_pad() function

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: array_multisort
Next: array_pop



PHP: Tips of the Day

PHP: Extract numbers from a string

$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);