PHP array_pad Function | Pad Array to a Specified Length

PHP array_pad is an inbuilt function in PHP. It returns an array of a specified length after padding it with a specific value. However, if the original size of the array is already greater than the specified length, it does not delete any elements. In this article, we will discuss the PHP array_pad function. Also, we will discuss a few examples of using it.

Syntax

array array_pad($input_array, $input_size, $values)

Parameters

The PHP array_pad Function expects three parameters. Also, all the parameters are mandatory. The description of the parameters is as follows:

  • $input_array: The first parameter to the function is the input array in which you need to add elements.
  • $input_size: The second parameter refers to the total size of the final array.
    • If the size is positive, the function adds elements at the end of the array.
    • If the size is negative, the function adds elements in front of the array.
  • $values: The third parameter is the value you want to add in the array. The function will add new elements only if input size if greater than original size of the array.

Return Value

The PHP array_pad Function returns an array after padding it up to the input size. If the input size is smaller than the original size of the array, then no padding takes place. Also, it is possible to add, at most 1048576 elements at a time.

Note: The function re-indexes numerical keys.

PHP array_pad function

Examples

Let’s discuss a few examples of using the array_pad function in PHP.

Example 1: Positive Input Size

For instance, consider an array containing 3 elements. We need to make it to 5 elements after adding a padding value.

<?php

	$testArray = array('one', 'two', 'three');
 //5 Passed as the total size of the final array
 //The array will be padded with default_value
	$paddedArray = array_pad($testArray, 5, 'default_value');
	print_r($paddedArray);
?>

We pass 5 as the total size of the final array. The function pads the original array with default_value.

OUTPUT:
Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => default_value
    [4] => default_value
)

Example 2: Negative Input Size

Similarly, we can pass a negative input size to the array_pad Function in PHP. The function adds new elements in the beginning of the array (before the original elements).

<?php

	$testArray = array('one', 'two', 'three');
	$paddedArray = array_pad($testArray, -5, 'default_value');
	print_r($paddedArray);
?>

In the above example, we pass -5 as the total size of the final array.

OUTPUT:
Array
(
    [0] => default_value
    [1] => default_value
    [2] => one
    [3] => two
    [4] => three
)

Example 3: Input Size Less Than Original Size

However, if the input size is less than the original size, the function returns the original array without any padding.

<?php

	$testArray = array('one', 'two', 'three');
    //Original Size of the Array: 3, Input size: 1
    //No padding takes place
	$paddedArray = array_pad($testArray, 1, 'default_value');
	print_r($paddedArray);
?>

The above code will print the following:

OUTPUT:
Array
(
    [0] => one
    [1] => two
    [2] => three
)

Conclusion

In conclusion, we discussed the PHP array_pad Function. You can read more about it on PHP Official Documentation. Additionally, you can learn about more PHP Array Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *