English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP array_pad() function usage and example

PHP Array Function Manual

The PHP array_pad() function fills a value into an array with a specified length

Syntax

array_pad($array, $pad_size, $pad_value);

Definition and Usage

array_pad() returns a copy of array and fills it with value pad_size specified length. If pad_size If positive, fill to the right of the array, if negative, start filling from the left. If pad_size The absolute value is less than or equal to the length of the array, there is no filling. It is possible to fill up to 1048576 Units.

Parameter

Serial NumberParameters and Description
1

array(Required)

It specifies an array.

2

pad_size(Required)

It specifies the number of elements in the array returned by the function.

3

pad_value(Required)

It specifies the value of the new elements in the array returned by the function.

Return Value

It returns a copy of an array whose size is specified by pad_size and the value pad_value.

Online Example

array_pad() Example

<?php
   $input1 = array("a" => "Horse", "b" => "Cat", "c" => "Dog");
   
   print_r(array_pad($input1,7, "COW")
?>
Test and See‹/›

Output Result:

Array
(
    [a] => Horse
    [b] => Cat
    [c] => Dog
    [0] => COW
    [1=> COW
    [2=> COW
    [3=> COW
)

 PHP Array Function Manual