|
|
Insert Array Elements in PHP
|
Tutorials » Php »
|
Topic |
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.
|
|
A Note |
Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial.
Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.
|
|
|
|