PHP/Data Structure/array pad
Содержание
- 1 Add new values to the beginning of the array by using a negative value for $size:
- 2 An array that is padded from the front
- 3 array_pad 1
- 4 array_pad 2
- 5 array_pad 3
- 6 array_pad() function expands an array to a precise size, padding it with a default value.
- 7 Pad array to the specified length with a value
- 8 Setting an Array"s Size: array array_pad(array $input, int $size, mixed $value)
- 9 Using array_pad() with associative arrays
Add new values to the beginning of the array by using a negative value for $size:
<source lang="html4strict">
<?php $birds = array("a", "b", "c", "d"); $more_birds = array_pad($birds, -6, "some bird"); printf("%s", var_export($more_birds, TRUE)); ?>
</source>
An array that is padded from the front
<source lang="html4strict">
<?
$weights = array (1, 3, 5, 10, 15, 25, 50); $weights = array_pad($weights, -10, 100); print_r($weights);
?>
</source>
array_pad 1
<source lang="html4strict">
<?php
$birds = array("parrot", "magpie", "lorakeet", "cuckoo"); $more_birds = array_pad($birds, 6, "some bird");printf("
More birds:
%s\n", var_export($more_birds, TRUE));
?>
</source>
array_pad 2
<source lang="html4strict">
<?php
$birds = array("parrot", "magpie", "lorakeet", "cuckoo"); $more_birds = array_pad($birds, -6, "some bird");printf("
More birds:
%s\n", var_export($more_birds, TRUE));
?>
</source>
array_pad 3
<source lang="html4strict">
<?php
$dogs = array("A" => "AA", "Bud" => "BB","C" => "D"); $pups = array_pad($dogs, 6, "mutt");printf("
Pups:
%s\n", var_export($pups, TRUE));
$pups = array_pad($dogs, -6, "mutt");printf("
Pups:
%s\n", var_export($pups, TRUE)); printf("
Dogs:
%s\n", var_export($dogs, TRUE));
?>
</source>
array_pad() function expands an array to a precise size, padding it with a default value.
<source lang="html4strict">
//Its syntax is: array array_pad(array array, int pad_size, mixed pad_value); pad_size specifies the new length of the array. pad_value parameter specifies the default value. If pad_size is positive, then the array will be padded to the right; If pad_size is negative, the array will be padded to the left. If the absolute value of pad_size is less than or equal to the length of the array, then no action will be taken. <?
$weights = array (1, 3, 5, 10, 15); $weights = array_pad($weights, 10, 100); print_r($weights);
?>
</source>
Pad array to the specified length with a value
<source lang="html4strict">
<? $input = array (12, 10, 9); $result = array_pad ($input, 5, 0); $result = array_pad ($input, -7, -1); $result = array_pad ($input, 2, "noop"); ?>
</source>
Setting an Array"s Size: array array_pad(array $input, int $size, mixed $value)
<source lang="html4strict">
<?php $birds = array("a", "b", "c", "d"); $more_birds = array_pad($birds, 6, "some bird"); printf("Birds:%s", var_export($birds, TRUE)); printf("More birds:%s", var_export($more_birds, TRUE)); ?>
</source>
Using array_pad() with associative arrays
<source lang="html4strict">
<?php $dogs = array("A" => "C", "B" => "D", "X" => "Z"); $pups = array_pad($dogs, 5, "mutt"); printf("right padding:%s", var_export($pups, TRUE)); $pups = array_pad($dogs, -5, "mutt"); printf("left padding:%s", var_export($pups, TRUE)); printf("Dogs:%s", var_export($dogs, TRUE)); ?>
</source>