Home » PHP

PHP array_pad() function with example

PHP array_pad() function: Here, we are going to learn about the array_pad() function with example in PHP.
Submitted by IncludeHelp, on February 22, 2019

PHP array_pad() function

array_pad() function is used to pad an array to given size with a specified value and returns a new array with a specified value.

Syntax:

    array_pad(array, size, value);

Here,

  • array is an array in which we have to add the elements.
  • size is the length/size of the array.
  • value is the value to be added to pad an array.

Examples:

    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! 
)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.