Insert Array Elements in PHP

What is array_pad() Function?

Explanation

The "array_pad()" function inserts a specified number of elements, with a specified value, to an array.

Syntax:


array_pad (array,size,value)

In the above syntax for insert the array elements is mandatory, "size" specifies the total number of elements to be added,"value" specifies the value for the elements.

Example :


<?php
$a=array("Fruit","Orange");
print_r(array_pad($a,4,Guava));
print "<br>";
print_r(array_pad($a,-3,Lemon));
?>
Result :

Array ([0] => Fruit [1] => Orange [2] => Guava [3] => Guava )
Array ([0] => Lemon [1] => Fruit [2] => Orange )

In the above example the total number of elements is 5,which is assigned to 0 apart from the elements already in the array.When assigned a negative value that is "-5" first the elements are added then the existing elements are displayed.

PHP Topics


Ask Questions

Ask Question