PHP array_pad() function with example

PHP array_pad() function with example

PHP array_pad() work: Here, we will find out about the array_pad() work with the example in PHP.

PHP array_pad() work

array_pad() work is utilized to pad a cluster to given size with a predetermined value and returns another exhibit with a predefined value.

Syntax:

array_pad(array, size, value); 

Here,

  • the exhibit is a cluster wherein we need to include the components.
  • size is the length/size of the cluster.
  • value is the value to be added to pad a cluster.

Example:

    Input:
    $arr = array(10, 20, 30);

    Function call:
    array_pad($arr, 5, 100);

    Output:
    Array
    (    
        [0] => 10   
        [1] => 20   
        [2] => 30   
        [3] => 100  
        [4] => 100  
    )

PHP Code:

<?php
    $arr = array(10, 20, 30);
    //padding to 5 elements with value 100
    $result = array_pad($arr, 5, 100);
    //printing
    print_r ($result);

    $arr = array("Hello", "Guys");
    //padding to 5 elements with value "Bye!"
    $result = array_pad($arr, 5, "Bye!");
    //printing
    print_r ($result);    
?>

Output:

Array
(    
    [0] => 10   
    [1] => 20   
    [2] => 30   
    [3] => 100  
    [4] => 100  
)    
Array
(    
    [0] => Hello
    [1] => Guys 
    [2] => Bye! 
    [3] => Bye! 
    [4] => Bye! 
)

Recent Posts

error: Alert: Content is protected!!