百科狗-知识改变命运!
--

array_pad() - 以指定长度将一个值填充进数组 - php 数组函数

是丫丫呀1年前 (2023-11-21)阅读数 27#技术干货
文章标签数组

array_pad()

(PHP 4, PHP 5, PHP 7)

以指定长度将一个值填充进数组

说明

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

array_pad()返回$array的一个拷贝,并用$value将其填补到$size指定的长度。如果$size为正,则填补到数组的右侧,如果为负则从左侧开始填补。如果$size的绝对值小于或等于$array数组的长度则没有任何填补。有可能一次最多填补 1048576 个单元。

参数

$array

需要被填充的原始数组。

$size

新数组的长度。

$value

将被填充的值,只有在$array的现有长度小于$size的长度时才有效。

返回值

返回$array用$value填充到$size指定的长度之后的一个副本。如果$size为正,则填补到数组的右侧,如果为负则从左侧开始填补。如果$size的绝对值小于或等于$array数组的长度则没有任何填补。

范例

array_pad() - 以指定长度将一个值填充进数组 - php 数组函数

Example #1array_pad()例子

参见

  • array_fill() 用给定的值填充数组
  • range() 根据范围创建数组,包含指定的元素
Beware, if you try to pad an associative array using numeric keys, your keys will be re-numbered.

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
)
little older, a little wiser.
ksort() will order the array back into its normal order again
so:

Will give : 
Array ( [0] => FILLED [1] => FILLED [2] => two [3] => FILLED [4] => four [5] => FILLED )
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...

will return:
Array ( [0] => FILLED [1] => FILLED [2] => two [3] => three [4] => FILLED [5] => FILLED )
The ordering should be okay,...
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.
One way to initialize a 20x20 multidimensional array. 
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...
OR you could do this 

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
Easy way to get an array contains 5 random numbers from 0 to 9:
$rand_arr = array_rand( array_pad( array(), 10, 1 ), 5 );
// insert element to array
function array_insert(&$arr, $pos, $new_el=null) {
  $arraypad = array_pad($arr, count($arr)+1, 0);
  for ($i=count($arr)-1; $i>=$pos; $i--) {
    $arr[$i+1] = $arr[$i];
    if ($i == $pos) {
      $arr[$i] = $new_el;
    }
  }
}
$digits = array();
$digits[0] = 0;
$digits[1] = 1;
$digits[2] = 2;
$digits[3] = 3;
$digits[4] = 4;
$digits[5] = 5;
echo "was: "; var_dump($digits);
array_insert($digits, 3, 100);
echo "new: "; var_dump($digits);
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:

output:
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )
Just an info about the value of "$pad_size" , 
If we set the value of "$pad_size" from -3 to 3,
It will produce the output like:

means array will remain the same.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)