|
 |
array_pad (PHP 4, PHP 5) array_pad --
Увеличить размер массива до заданной величины
Описаниеarray array_pad ( array input, integer pad_size, mixed pad_value )
Функция array_pad() возвращает копию массива
исходный_массив, размер которого был
увеличен до значения параметра размер элементами
со значением значение. Если параметр
размер является положительным числом,
то массив увеличивается с конца, если отрицательный - сначала.
Если абсолютное значение параметра размер
меньше или равно размеру массива исходный_массив,
функция не производит никаких операций.
Пример 1. Пример использования array_pad()
$input = array (12, 10, 9);
$result = array_pad ($input, 5, 0);
// результат: array (12, 10, 9, 0, 0)
$result = array_pad ($input, -7, -1);
// результат: array (-1, -1, -1, -1, 12, 10, 9)
$result = array_pad ($input, 2, "noop");
// операция не произведена
|
|
array_pad
oaev at mail dot ru
21-Oct-2004 11:48
Easy way to get an array contains 5 random numbers from 0 to 9:
$rand_arr = array_rand( array_pad( array(), 10, 1 ), 5 );
28-Feb-2004 09:00
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);
?>
mwwaygoo at hotmail dot com
16-Jan-2004 08:02
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 )
goffrie at sympatico dot ca
23-Mar-2003 05:06
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,...
mwwaygoo at hotmail dot com
19-Sep-2002 09:39
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
daarius at hotmail dot com
23-Jul-2002 07:36
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...
scott*hurring.com
19-Jul-2002 04:20
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.
ethanhunt314 at hotmail dot com
10-Dec-2000 04:25
This is useful when using next() and prev() function in a while loop to traverse an array.
For example the following code will only output up to 8.
<?php
$test[] = "1";
$test[] = "2";
$test[] = "3";
$test[] = "4";
$test[] = "5";
$test[] = "6";
$test[] = "7";
$test[] = "8";
$test[] = "9";
$test[] = "10";
$test[] = " ";
$test[] = " ";
$test[] = " ";
$count = count($test);
while($i < $count) {
$now = current($test);
echo "<p>$now</p>";
next($test);
next($test);
next($test);
prev($test);
prev($test);
prev($test);
$i++;
next($test);
}
?>
But if you use:
$test = array_pad($test, 13, " ");
you will get all of your output.
| |