Definition
The array_pad() function inserts a value into an array up to the specified length.
Syntax
array_pad(array, size, value)
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. |
Example
<?php
// Example 1
$cities = array("New York", "Salt Lake", "Tokyo");
print_r(array_pad($cities, 5, "Berlin"));
echo "<br>";
// Example 2
$cities = array("New York", "Salt Lake", "Tokyo");
print_r(array_pad($cities, -3, "Berlin"));
echo "<br>";
// Example 3
$cities = array("New York", "Salt Lake", "Tokyo");
print_r(array_pad($cities, 1, "Berlin"));
echo "<br>";