Home » 
        PHP
    
    PHP Array array_pad() Function (With Examples)
    
    
    
    
        In this tutorial, we will learn about the PHP array_pad() function with its usage, syntax, parameters, return value, and examples.
        
            By IncludeHelp Last updated : December 31, 2023
        
    
    PHP array_pad() function
    The 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
The syntax of the array_pad() function:
array_pad(array, size, value);
Parameters
The parameters of the array_pad() function:
    - 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.
 
Return Value
The return type of this method is array, it returns a copy of the array padded to size specified by length with value value. [Source]
Sample Input/Output
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 array_pad() Function Example
<?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
The output of the above example is:
Array
(    
    [0] => 10   
    [1] => 20   
    [2] => 30   
    [3] => 100  
    [4] => 100  
)    
Array
(    
    [0] => Hello
    [1] => Guys 
    [2] => Bye! 
    [3] => Bye! 
    [4] => Bye! 
)
	
    
    
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement