PHP | array_pad() Function

The array_pad() is a builtin function in PHP and is used to pad a value fixed number of time onto an array. This function inserts an element specified number of times into an array either at front or back.

Syntax:

array array_pad($input_array, $input_size, $values)

Parameters: This function accepts three parameters, all of this which are mandatory to be supplied.



  1. $input_array (mandatory): Refers to the array, on which the operation is to be performed, or to which the elements are needed to be added.
  2. $total_size (mandatory): Refers to the total size of the new array to be returned.
    • If the value is positive, the elements get added to the end of the array.
    • If the value is negative, the elements get added at the beginning of the array.
  3. $values (mandatory): Refers to the value with which padding will take place. The padding takes place only when $total_size is greater than the size of the input_array.

Return Value: The function returns a copy of the array padded to the size of $total_size. If the absolute value of $total_size is less than or equal to the length of the array then no padding takes place. It is possible to add, at most 1048576 elements at a time.

Examples:

Input : array = ("one", "two", "three", "four", "five")
        $total_size  = 7 , $value = "six"
Output : 
Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
    [5] => six
    [6] => six
)

Input : array = ("one", "two", "three", "four", "five")
        $total_size  = -7 , $value = "six"
Output : 
Array
(
    [0] => six
    [1] => six
    [2] => one
    [3] => two
    [4] => three
    [5] => four
    [6] => five
)

Below programs explains the working of the array_pad() function:

  1. Padding elements at end of the array when the $total_size is positive:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // PHP function to illustrate the use of array_pad()
    function Padding($array, $string)
    {
        $result = array_pad($array, 7, $string);
        return($result);
    }
      
    $array = array("one", "two", "three", "four", "five");
    $string = "six";
    print_r(Padding($array, $string));
    ?>

    chevron_right

    
    

    Output:

    Array
    (
        [0] => one
        [1] => two
        [2] => three
        [3] => four
        [4] => five
        [5] => six
        [6] => six
    )
    
  2. Padding elements at start of the array when the $total_size is negative:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // PHP function to illustrate the use of array_pad()
    function Padding($array, $string)
    {
        $result = array_pad($array, -7, $string);
        return($result);
    }
      
    $array = array("one", "two", "three", "four", "five");
    $string = "six";
    print_r(Padding($array, $string));
    ?>

    chevron_right

    
    

    Output:

    Array
    (
        [0] => six
        [1] => six
        [2] => one
        [3] => two
        [4] => three
        [5] => four
        [6] => five
    )
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.