Array Functions
Advertisements

PHP array_pad() Function

Topic: PHP Array ReferencePrev|Next

Description

The array_pad() function inserts a value into an array up to the specified length.

The following table summarizes the technical details of this function.

Return Value: Returns an array padded with new elements.
Version: PHP 4+

Syntax

The basic syntax of the array_pad() function is given with:

array_pad(array, size, value);

The following example shows the array_pad() function in action.

<?php
// Sample array
$numbers = array(5, 10, 15);
    
// Padding numbers array 
print_r(array_pad($numbers, 5, 0));
?>

Parameters

The array_pad() function accepts the following parameters.

Parameter Description
array Required. Specifies the array in which to insert the elements.
size Required. Specifies the new size of the array.
value Required. Specifies the value to insert if array's length is less than the size.

More Examples

Here're some more examples showing how array_pad() function actually works:

If size is positive then the array is padded on the right, if it's negative then padded on the left.

<?php
// Sample array
$numbers = array(5, 10, 15);
    
// Padding numbers array
print_r(array_pad($numbers, -5, 0));
?>

If the absolute value of size is less than or equal to the array's length then no padding takes place.

<?php
// Sample array
$numbers = array(5, 10, 15);
    
// Padding numbers array 
print_r(array_pad($numbers, 2, 0));
?>
Advertisements
Bootstrap UI Design Templates

Your Feedback:

We would love to hear from you, please drop us a line.