PHP array_fill():以填充数据的方式创建新数组
2023-06-13 09:12:02 时间
PHP array_fill() 函数会以填充数据的方式创建一个新数组,或者说使用某个值将数组填充到指定的长度,其语法如下:
如果 start_index 为非负数,数组的索引将从 start_index 开始,直到 start_index + num 1 结束; 如果 start_index 为负数,数组的索引将由 start_index, 0, 1, 2, , num-1 组成。
array array_fill ( int $start_index , int $num , mixed $value )
参数说明:
如果 start_index 为非负数,数组的索引将从 start_index 开始,直到 start_index + num 1 结束; 如果 start_index 为负数,数组的索引将由 start_index, 0, 1, 2, , num-1 组成。
array_fill() 将会创建一个新数组,该数组包含 num 个元素,每个元素的值都是 value。和 array_pad() 不同,array_fill() 会以填充的方式创建一个新数组,而不是基于原有数组。
返回值:返回填充后的新数组。
使用示例如下:
?php $a = array_fill(5, 6, banana //使用banana填充数组到6个元素, 索引键由数字5开始 $b = array_fill(-2, 4, pear //使用pear填充数组到4个元素, 索引键由-2开始 $c = array_fill(3, 2, array( PHP , http://c.biancheng.net/php/ , C语言中文网 // 用一个数组填充成一个二维数组 print_r($a); print_r($b); print_r($c); ?
运行结果如下:
Array
(
[5] = banana
[6] = banana
[7] = banana
[8] = banana
[9] = banana
[10] = banana
)
Array
(
[-2] = pear
[0] = pear
[1] = pear
[2] = pear
)
Array
(
[3] = Array
(
[0] = PHP
[1] = http://c.biancheng.net/php/
[2] = C语言中文网
)
[4] = Array
(
[0] = PHP
[1] = http://c.biancheng.net/php/
[2] = C语言中文网
)
)
23851.html
C语言htmlphp相关文章
- php 删掉空的数组 json数据. 空数据(false 0 ““ null)
- PHP之与或非_php字符串运算符
- PHP循环输出二维数组的数据
- PHP Fatal error: Uncaught Error: Call to undefined function pcntl_fork().. 开启php pcntl扩展实现多进程详解编程语言
- PHP array_fill_keys():使用指定的键和值来填充数组
- 数据如何使用PHP在MySQL中添加数据(php添加mysql)
- PHP连接MySQL:实现数据交互(php与mysql连接)
- MySQL事务处理流程及PHP实现(mysql事务php)
- 利用PHP操作Redis变得更简单(php中redis的使用)
- 教你如何用PHP获取MySQL数据库中的数据(php获取mysql数据)
- 轻松实现高效数据调用:PHP如何与Redis集成(php调用redis)
- 使用PHP连接MySQL数据库实现数据操作(php连接mysql类)
- 如何使用MySQL快速查询PHP数组(mysql查询数组php)
- 安装Linux实现一键安装PHP环境(linux一键php)
- PHP实现从MSSQL中数据显示(php 显示 mssql)
- PHP与MSSQL的良好结合,发挥强大的功能(php和mssql)
- MySQL与PHP开发之旅(mysql与php书籍)
- Redis实现PHP限流策略(redis限流php)
- 使用php重新实现PHP脚本引擎内置函数
- PHP以指定字段为索引返回数据库所取的数据数组
- php读取csv数据保存到数组的方法