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 −

 Live Demo

<?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
)

Updated on: 23-Dec-2019

382 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements