Computer >> Computer tutorials >  >> Programming >> PHP

array_pad() function in PHP


The array_pad() function inserts a specified number of items, with a specified value, to an array. It returns array with new elements. If size is positive then the array is padded on the right, if it's negative then the padding is done on the left.

Syntax

array_pad(arr, size, value)

Parameters

  • arr − the array

  • size − total elements in the resultant array

  • value − the value to pad and it should be less than the size of arr

Return

The array_pad() function returns array with new elements. If size is positive then the array is padded on the right, if it's negative then the padding is done on the left.

Example

The following is an example −

<?php
$arr = array("one","two");
print_r(array_pad($arr,4,"three"));
?>

Output

The following is the output −

Array (
[0] => one
[1] => two
[2] => three
[3] => three
)