Tutorialspoint.dev

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:

    <?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));
    ?>

    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:

    <?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));
    ?>

    Output:

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


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter