w3resource

PHP: array_pad() function

PHP: Pad array to the specified length with a value

The array_pad() function is used to padding (insert) a specified number of elements, with a specified value, into an array.
If the specified value is positive then the array is padded on the right, if it is negative then padded on the left.
If the specified value is less than or equal to the length of the input then no padding takes place.

Version

(PHP 4 and above)

Syntax

array array_pad (input_array, pad_size, pad_value)

Parameters

Name Description Required /
Optional
Type
input_array Specifies the name of the array. Required Array
pad_size A total number of elements in the resulting array after padding. Required Array
pad_value Value with which padding will take place, must be less than the size of the input_array. Required Mixed*

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value

An array with new elements.

Value Type : Array

Example:

<?php
$input_array= array(1,7,10,6);
$result=array_pad($input_array,5,330);
print_r($result);
?>

Output:

Array ( [0] => 1 [1] => 7 [2] => 10 [3] => 6 [4] => 330  ) 

Pictorial Presentation:

php function reference: array_pad() function

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: array_multisort
Next: array_pop



Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

PHP: How to define an empty object in PHP?

$x = new stdClass();

stdClass is the default PHP object. stdClass has no properties, methods or parent. It does not support magic methods, and implements no interfaces.

When you cast a scalar or array as Object, you get an instance of stdClass. You can use stdClass whenever you need a generic object instance.

Ref : https://bit.ly/2Q96Wvm

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook