array_pad() function in php
×


array_pad() function in php

1789

array_pad() function of PHP is used to add a certain number of items to the existing array with a user define value and return a new array. It is an inbuilt function of PHP.

Syntax:

array_pad ($array,$size,$value);

here,

  • $array is a PHP array.
  • $size is the number of elements to the new array.
  • $value is the value of the new inserted elements.

  • Example 1:

    <?php
    $arr=array("Apple","Mango","Grapes");
    $result=array_pad($arr,6,"pear");
    echo "<pre>";
    print_r($result);
    ?>

    Output:


    Example 2:

    <?php
    // array_pad() function with associative array
    $arr=array("fruit1"=>"Apple","fruit2"=>"Mango","fruit3"=>"Grapes");
    $result=array_pad($arr,6,"pear");
    echo "<pre>";
    print_r($result);
    ?>

    Output:




Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments