Jump to: navigation, search

Php array pad

From w3cyberlearnings

Contents

PHP function array_pad

This function inserts array to the specified length with a value.

Syntax array_pad

  • array: array to be pad
  • length: number of elements in the array
  • value: new value element to be inserted.
array_pad(array, length, value);

Example 1

<?php

$pet = array("dog", "cat");
$pets = array_pad($pet, 4, "Tommy");
print_r($pets);
?>

Output

Array
(
    [0] => dog
    [1] => cat
    [2] => Tommy
    [3] => Tommy
)

Example 2: negative width

  • When the length is negative it adds from the front.
<?php

$pet = array("dog", "cat");
$pets = array_pad($pet, -4, "Tommy");
print_r($pets);
?>

Output

Array
(
    [0] => Tommy
    [1] => Tommy
    [2] => dog
    [3] => cat
)

Related Links


Navigation
Web
SQL
MISC
References