Wednesday, 24 September 2014

array_pad in PHP

array_pad() function simulates the existing array and pads the named size and value at the end of the array then returns a result in the format of the array.

Syntax:

array_pad(array,size,value)

Parameters Description:
array : Required. Define an array.
size : Required. Define the number of elements in the array generated by the function.
value : Required. Define the value of the new elements in the array generated by the function.

Tip : If negative sized parameter is assigned, the function will insert new elements In Front the original elements.

Note : If the original array's size is greater than the size parameter then this function will not delete any elements.

Example:

<?php
$daysarr1 = array( "Sunday","Monday" );
print_r( array_pad($daysarr1,4,0) );
$daysarr2 = array("Tuesday","Wednesday");
echo "<br />"."using a negative size parameter";
print_r( array_pad($daysarr2,-4,0) );
?>

O/P:


Array ( 
            [0] => Sunday
            [1] => Monday 
            [2] => 0
            [3] => 0
         )
using a negative size parameter
Array (
           [0] => 0
           [1] => 0 
           [2] => Tuesday 
           [3] => Wednesday 
         )

0 comments:

Post a Comment