PHP array_pad() Function

PHP array_pad() Function

The array_pad() function in PHP pads the array to the given length and value and returns a copy of the array padded to size and value specified by size and value parameter. The padding takes place as follows:

  • If the parameter “size” is positive, then the array is padded on the right, if it's negative then on the left. 
  • If “size” is less than or equal to the size of the array, no padding takes place.

Syntax

array_pad ( array $array , int $size , mixed $value )

Parameter

array(required)- This parameter represents the input arrays to pad.

size(required)- This parameter signifies the new size of the array.

value(required)- This parameter represents the value to pad if the specified array is less than the given size.

Return

This function returns a copy of the array padded to size and value specified by size and value parameter.

Example 1

 

Output

Array
 (
     [0] => 102
     [1] => 10
     [2] => 9
     [3] => 10
 ) 
 After padding the array: 
 Array
 (
     [0] => 102
     [1] => 10
     [2] => 9 
     [3] => 10
     [4] => NA
     [5] => NA
 ) 

Example 2

 

Output

Array
 (
     [0] => 102
     [1] => 10
     [2] => 9
     [3] => 10
 )
 After padding the array:  
 Array
 (
     [0] => NA
     [1] => NA
     [2] => NA
     [3] => 102
     [4] => 10
     [5] => 9 
     [6] => 10
 ) 

Example 3

 

Output

Array
 (
     [0] => Reema
     [1] => Rahul
     [2] => Riya
     [3] => Varun
     [4] => Sukla
     [5] => Amar 
     [6] => Amar
 ) 

Related Topics

PHP number_format() Function

PHP number_format() Function The number_format() function in PHP formats a number with grouped thousands. This function accepts one, two, or four parameters (not three): Syntax number_format ( float $number [, int $decimals ] )             or number_format ( float $number , int $decimals  , string $dec_point, string$thousands_sep ) Parameter number(required)- This parameter represents the number to be formatted. If...

1 minute read.

PHP is_long() Function

The PHP is_long () function in PHP is used to find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it returns...

1 minute read.

PHP array_change_key_case() Function

PHP array_change_key_case() Function The array_change_key_case() function in PHP is used to change the case of all keys present in the specified array. It returns an array with all keys in the specified array to lowercase or...

1 minute read.

PHP Comment

PHP commentis a line of code that is not executed. It is often used by developer to understand the code. PHP support single and multiple comments. In other language like C,...

1 minute read.

PHP array_intersect_ukey() function

The array_intersect_ukey () function in PHP is used to compute the intersection of arrays with the help of a callable function on the keys for comparison. It returns an array containing all the...

1 minute read.

PHP sscanf() Function

PHP sscanf() Function The sscanf() function in PHP parses input from a string according to a given format. Syntax sscanf ( string $str , string $format [,mixed &$arg1,$agr2,$arg++... ] ) Parameter str(required)- This parameter specifies the string to read. format(required)- This parameter specifies the format to use. The conversion specification...

3 minutes read.

PHP join() Function

PHP join() Function The join() function of PHP returns a string from the elements of an array. This function is an alias of the implode() function. Syntax join(separator,array) Parameter separator(Optional)- Specifies what to put between the array elements. The default...

1 minute read.

PHP prev() Function

PHP prev() Function The prev() function in PHP rewinds or moves back the internal array pointer one place backward and returns the prev array value. It behaves like next() function, except that this function rewinds the...

1 minute read.

PHP array_pop() Function

PHP array_pop() Function The array_pop() function in PHP pops or removes the last element from the array shortening the array by one element. Syntax array_pop ( array &$array ) Parameter array(required)- This parameter represents the input array to get the value...

1 minute read.

PHP serialize() Function

PHP serialize() Function The serialize() function in PHP is used to convert a storable representation of a value. Syntax serialize ( mixed $value ) Parameter value(required)- This parameter represents the value to be serialized. Return This function returns a string containing a byte-stream representation...

1 minute read.

PHP ksort() Function

PHP ksort() Function The ksort() function in PHP sorts an array by key while maintaining the keys. Syntax ksort(array & $array[,int $sort_flags = SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This parameter is...

2 minutes read.

PHP trim() Function

PHP trim() Function The trim() Function in PHP strips the whitespace or other characters from the beginning and the end of the given string. The trim() function strips the following characters: " " - an ordinary space."\t"- a tab."\n"- a new...

1 minute read.

PHP Environment Setup

To install PHP, We have to installed AMP(Apache, MySQL and PHP) software. There are various AMP software available in the market. XAMPP (Cross-Platform, Apache, MySQL, PHP and Perl) for windows. ...

2 minutes read.

PHP array_sum() Function

PHP array_sum() Function The array_sum() function in PHP is used to calculate the sum of values in an array. Syntax array_sum ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns the sum of values as an...

1 minute read.

PHP intval() Function

The intval() Function in PHP is used to get the integer value for the given variable. This function should not be used on objects. Else it will emit an E_NOTICE level error and return 1. Syntax intval...

1 minute read.

PHP str_ireplace() Function

PHP str_ireplace() Function The PHP str_ireplace() function in PHP replaces some characters with some other characters in a string. Syntax str_ireplace ( mixed $search , mixed  $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter specifies the value being searched for. replace(required)- This parameter represents the replacement value...

1 minute read.

PHP vsprintf() Function

PHP vsprintf() Function The vsprintf() function in PHP returns a formatted string. This function accepts an array of arguments. Syntax vsprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to format the variables in it. The conversion specification of this parameter...

3 minutes read.

PHP Tutorial

PHP Tutorial PHP Introduction PHP is a server-side scripting language which is used for web development. It stands for Hypertext Preprocessor. It is faster than another programming language. It supports multiple databases like MySQL, SQL, PostgreSQL and...

3 minutes read.

PHP strip_tags() Function

PHP strip_tags() Function The strip_tags() function in PHP is sued to strip a string from HTML, XML, and PHP tags. It is a binary-safe function. Syntax strip_tags ( string $str [, string $allowable_tags ] ) Parameter str(required)- This parameter specifies the input string. allowable_tags(optional)- This parameter represents...

1 minute read.

PHP crypt() Function

PHP crypt() Function The crypt () function in PHP returns a hashed string using DES, Blowfish, or MD5 algorithms. Some constants of crypt() function are as follows: [CRYPT_STD_DES][CRYPT_EXT_DES][CRYPT_MD5][CRYPT_BLOWFISH][CRYPT_SHA_256][CRYPT_SHA_512] etc. Syntax crypt ( string $str [, string $salt ] )  Parameter str(required)- This parameter represents the string to be hashed salt(optional)- This parameter...

2 minutes read.