array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'function.array-pad.php', 1 => 'array_pad', 2 => 'Pad array to the specified length with a value', ), 'up' => array ( 0 => 'ref.array.php', 1 => 'Функції для роботи з масивами', ), 'prev' => array ( 0 => 'function.array-multisort.php', 1 => 'array_multisort', ), 'next' => array ( 0 => 'function.array-pop.php', 1 => 'array_pop', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/array/functions/array-pad.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

array_pad

(PHP 4, PHP 5, PHP 7, PHP 8)

array_padPad array to the specified length with a value

Опис

array_pad(array $array, int $length, mixed $value): array

array_pad() returns a copy of the array padded to size specified by length with value value. If length is positive then the array is padded on the right, if it's negative then on the left. If the absolute value of length is less than or equal to the length of the array then no padding takes place.

Параметри

array

Initial array of values to pad.

length

New size of the array.

value

Value to pad if array is less than length.

Значення, що повертаються

Returns a copy of the array padded to size specified by length with value value. If length is positive then the array is padded on the right, if it's negative then on the left. If the absolute value of length is less than or equal to the length of the array then no padding takes place.

Журнал змін

Версія Опис
8.3.0 Prior to 8.3, only 1048576 elements could be added at once. Now it is limited only by the maximum size of an array instead.

Приклади

Приклад #1 array_pad() example

<?php
$input
= array(12, 10, 9);

$result = array_pad($input, 5, 0);
// result is array(12, 10, 9, 0, 0)
echo join(', ', $result), PHP_EOL;

$result = array_pad($input, -7, -1);
// result is array(-1, -1, -1, -1, 12, 10, 9)
echo join(', ', $result), PHP_EOL;

$result = array_pad($input, 2, "noop");
// not padded
echo join(', ', $result), PHP_EOL;
?>

Прогляньте також