phpday 2027 - Call for Papers

array_pad

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

array_pad — Riempie un array fino alla lunghezza specificata con un valore

Descrizione

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

array_pad() restituisce una copia di array riempita fino alla dimensione specificata da length con il valore value. Se length è positivo, l'array viene riempito a destra; se è negativo, a sinistra. Se il valore assoluto di length è minore o uguale alla lunghezza di array, non viene aggiunto alcun elemento.

Elenco dei parametri

array

Array iniziale di valori da riempire.

length

Nuova dimensione dell'array.

value

Valore da aggiungere se array è più corto di length.

Valori restituiti

Restituisce una copia di array riempita fino alla dimensione specificata da length con il valore value. Se length è positivo, l'array viene riempito a destra; se è negativo, a sinistra. Se il valore assoluto di length è minore o uguale alla lunghezza di array, non viene aggiunto alcun elemento.

Log delle modifiche

Versione Descrizione
8.3.0 Prima della 8.3, potevano essere aggiunti solo 1048576 elementi alla volta. Ora il limite è solo la dimensione massima di un array.

Esempi

Example #1 Esempio di array_pad()

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

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

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

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

Vedere anche:

  • array_fill() - Riempie un array con dei valori
  • range() - Crea un array contenente un intervallo di elementi

User Contributed Notes 8 notes

up
20
tugla
17 years ago
Beware, if you try to pad an associative array using numeric keys, your keys will be re-numbered.

<?php
$a = array('size'=>'large', 'number'=>20, 'color'=>'red');
print_r($a);
print_r(array_pad($a, 5, 'foo'));

// use timestamps as keys
$b = array(1229600459=>'large', 1229604787=>20, 1229609459=>'red');
print_r($b);
print_r(array_pad($b, 5, 'foo'));
?>

yields this:
------------------
Array
(
    [size] => large
    [number] => 20
    [color] => red
)
Array
(
    [size] => large
    [number] => 20
    [color] => red
    [0] => foo
    [1] => foo
)
Array
(
    [1229600459] => large
    [1229604787] => 20
    [1229609459] => red
)
Array
(
    [0] => large
    [1] => 20
    [2] => red
    [3] => foo
    [4] => foo
)
up
13
goffrie at sympatico dot ca
23 years ago
To daarius - you mean you have...

[2]=>"two"
[3]=>"three"

and you want...

[0]=>"FILLED"
[1]=>"FILLED"
[2]=>"two"
[3]=>"three"
[4]=>"FILLED"
[5]=>"FILLED"

If so, then the following code...

<?php
$array = array(2 => "two", 3 => "three");
$array = array_pad($array, count($array)+2, "FILLED");
$num = -(count($array)+2);
$array = array_pad($array, $num, "FILLED");
print_r($array);
?>

will return:
Array ( [0] => FILLED [1] => FILLED [2] => two [3] => three [4] => FILLED [5] => FILLED )
The ordering should be okay,...
up
7
scott*hurring.com
24 years ago
to the previous commenter -- if you read the manual entry, you'd see that a negative pad_size will put the pad values at the front of the array.
up
6
mwwaygoo at hotmail dot com
22 years ago
little older, a little wiser.

ksort() will order the array back into its normal order again
so:

<?php
$myArr = array(2 => 'two', 4 => 'four');

$newArr = array_pad(array(), 6, 'FILLED');
$newArr =$myArr+$newArr;
ksort($newArr);
?>

Will give : 
Array ( [0] => FILLED [1] => FILLED [2] => two [3] => FILLED [4] => four [5] => FILLED )
up
1
Anonymous
22 years ago
One way to initialize a 20x20 multidimensional array.  

<?php
$a = array();
$b = array();
$b = array_pad($b,20,0);
$a = array_pad($a,20,$b);
?>
up
0
hk, StrApp Bussiness Solutions
19 years ago
A simple example for array_pad()

the syntax is as follows: array_pad(array(), (+/-)int, value)

where "array" is the array to which the value is to be added,

"(+/-) int" is a value that decides the length of the array(it should be greater than the length of the array.
if its a negative number then the value will be added at the left of the array else it will be added to the right.

"values" denotes the value to be added to the array

lets try an example:

<?php

$digits = array();
$digits[0] = 1;
$digits[1] = 2;
$digits[2] = 3;
$arraypad = array_pad($digits, -4, "0");
print_r($arraypad);

?>

output:

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )
up
-1
daarius at hotmail dot com
24 years ago
yes that is true. But, if the index of the array is 2=two, 3=three

and i want 4 more keys to be filled. But, not just filled anywhere, but i want to maintain the key index.

so, i would like to have 0=FILLED, 1=FILLED ... 4=FILLED, 5=FILLED

now i got 4 more keys padded with my string.

We can do this "if" we know the missing keys, but if we dont, then it would be nice for array_pad() or perhaps some new function to do this?

obviously we can achive this by looping through the array using array_key_exists(), and if you dont find the key, simply create + fill it.
regards,
Daarius...
up
-2
mwwaygoo at hotmail dot com
24 years ago
OR you could do this 

<?php
$myArr = array(2 => 'three', 3 => 'four');

$newArr = array_pad(array(), 4, 'FILLED');
$newArr =$myArr+$newArr;
?>

This gives your desired result BUT the ordering is a little wierd, because of the order they were added. Indexes are okay though and that is what you wanted.

print_r($newArr) outputs
Array ( [2] => three [3] => four [0] => FILLED [1] => FILLED )

hope this helps
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top