PHP/Data Structure/array pad — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:02, 26 мая 2010
Содержание
- 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:
<?php
$birds = array("a", "b", "c", "d");
$more_birds = array_pad($birds, -6, "some bird");
printf("%s", var_export($more_birds, TRUE));
?>
An array that is padded from the front
<?
$weights = array (1, 3, 5, 10, 15, 25, 50);
$weights = array_pad($weights, -10, 100);
print_r($weights);
?>
array_pad 1
<?php
$birds = array("parrot", "magpie", "lorakeet", "cuckoo");
$more_birds = array_pad($birds, 6, "some bird");
printf("<p>More birds:</p><pre>%s</pre>\n", var_export($more_birds, TRUE));
?>
array_pad 2
<?php
$birds = array("parrot", "magpie", "lorakeet", "cuckoo");
$more_birds = array_pad($birds, -6, "some bird");
printf("<p>More birds:</p><pre>%s</pre>\n", var_export($more_birds, TRUE));
?>
array_pad 3
<?php
$dogs = array("A" => "AA", "Bud" => "BB","C" => "D");
$pups = array_pad($dogs, 6, "mutt");
printf("<p>Pups:</p><pre>%s</pre>\n", var_export($pups, TRUE));
$pups = array_pad($dogs, -6, "mutt");
printf("<p>Pups:</p><pre>%s</pre>\n", var_export($pups, TRUE));
printf("<p>Dogs:</p><pre>%s</pre>\n", var_export($dogs, TRUE));
?>
array_pad() function expands an array to a precise size, padding it with a default value.
//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);
?>
Pad array to the specified length with a value
<?
$input = array (12, 10, 9);
$result = array_pad ($input, 5, 0);
$result = array_pad ($input, -7, -1);
$result = array_pad ($input, 2, "noop");
?>
Setting an Array"s Size: array array_pad(array $input, int $size, mixed $value)
<?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));
?>
Using array_pad() with associative arrays
<?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));
?>