Home >>PHP Array Functions >PHP array_pad() Function

PHP array_pad() Function

PHP array_pad() Function

PHP array_pad() Function is basically used in order to insert a specified number of elements, with a specified value to an array. It is considered to better if the user or programmer assigns a negative size parameter then the function will insert some new elements just before the original elements. To known more details you can refer to the example that has been explained below.

Please note that this function will not delete any elements in case the size of parameter is found to be less than the size of the original array.

Syntax:

array_pad(array, size, value)

Parameter Values

Parameter Description
array This is generally used to specify an array and it is required.
size This is generally used to specify the number of elements in the array that are returned from the function, it is a mandatory parameter.
value This is generally used to specify the value of the new elements in the array that are returned from the function and it is required.

Here, is an example of array_pad Function in PHP:

<html>
<body>
<?php
$a=array("white","black");
print_r(array_pad($a,5,"green"));
?>
</body>
</html>
Output:
Array ( [0] => white [1] => black [2] => green [3] => green [4] => green )

Example 2:

<html>
<body>
<?php
$a=array("green","pink");
print_r(array_pad($a,-7,"white"));
?>
</body>
</html>
Output:
Array ( [0] => white [1] => white [2] => white [3] => white [4] => white [5] => green [6] => pink )

No Sidebar ads