无为清净楼资源网 Design By www.qnjia.com
今天学习了数组,可以说是PHP的数据应用中较重要的一种方式。PHP的数组函数众多,下面是我学习的小结,借此记之,便于以后鉴之……
一、数组定义:
数组的定义使用 array()方式定义,可以定义空数组:
复制代码 代码如下:
<?php
$number = array(1,3,5,7,9);
//定义空数组
$result = array();
$color =array("red","blue","green");
//自定义键值
$language = (1=>"English",3=>"Chinese",5=>"Franch");
//定义二维数组
$two = array(
"color"=>array("red","blue"), //用逗号结尾
"week"=>array("Monday","Friday") //最后一句没有标点
);
?>
二、创建数组:
创建数组包含的函数有compact()、
1.compact()函数——将一个或多个变量(包含数组)转换为数组:
array compact ( mixed $varname [, mixed $... ] )
复制代码 代码如下:
<?PHP
$number = "1,3,5,7,9";
$string = "I'm PHPer";
$array = array("And","You?");
$newArray = compact("number","string","array");
print_r ($newArray);
?>
compact()函数用于将两个或多个变量转换为数组,当然也包含数组变量。其参数是变量的名称而非带有$全名。
相反的函数是extract()作用顾名思义就是将数组转换为单个的字符串,键值作为其字符串名称,数组值作为字符串的值。
运行结果:
复制代码 代码如下:
Array ( [number] => 1,3,5,7,9 [string] => I'm PHPer [array] => Array ( [0] => And [1] => You? ) )
2.array_combine()——将两个数组重组成一个数组,一个作键值一个做的值:
array array_combine ( array $keys , array $values )
复制代码 代码如下:
<?PHP
$number = array("1","3","5","7","9");
$array = array("I","Am","A","PHP","er");
$newArray = array_combine($number,$array);
print_r ($newArray);
?>
array_combine函数不多说了,谁看了都明白
运行结果:
Array ( [1] => I [3] => Am [5] => A [7] => PHP [9] => er )
3.range()函数——创建指定范围的数组:
不多说了,直接上实例——
复制代码 代码如下:
<?PHP
$array1 = range(0,100,10);//0为起始值,100为结束值,10为步进值(默认步进值为1).
print_r($array1);
echo"<br />";
$array2 = range("A","Z");
print_r($array2);
echo "<br />";
$array3 = range("z","a");
print_r($array3);
?>
range()函数的默认步进值是1!
运行结果:
复制代码 代码如下:
Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 [6] => 60 [7] => 70 [8] => 80 [9] => 90 [10] => 100 )
Array ( [0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6] => G [7] => H [8] => I [9] => J [10] => K [11] => L [12] => M [13] => N [14] => O [15] => P [16] => Q [17] => R [18] => S [19] => T [20] => U [21] => V [22] => W [23] => X [24] => Y [25] => Z )
Array ( [0] => z [1] => y [2] => x [3] => w [4] => v [5] => u [6] => t [7] => s [8] => r [9] => q [10] => p [11] => o [12] => n [13] => m [14] => l [15] => k [16] => j [17] => i [18] => h [19] => g [20] => f [21] => e [22] => d [23] => c [24] => b [25] => a )
4.array_fill()函数——填充数组函数:
复制代码 代码如下:
<?PHP
$array = range(1,10);
$fillarray = range("a","d");
$arrayFilled = array_fill(0,5,$fillarray);//这里的$fillarray可以是字符串,如"test".
echo "<pre>";
print_r ($arrayFilled);
echo "</pre>";
$keys = array("string","2",9,"SDK","PK");
$array2 = array_fill_keys($keys,"testing");
echo "<pre>";
print_r ($array2);
echo "</pre>";
?>
运行结果:
复制代码 代码如下:
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[2] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[3] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[4] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
)
Array
(
[string] => testing
[2] => testing
[9] => testing
[SDK] => testing
[PK] => testing
)
二、数组的遍历:
1.foreach遍历:
foreach (array_expression as $value){}
foreach (array_expression as $key => $value){}
闲话少说,上实例:
复制代码 代码如下:
<?PHP
$speed = array(50,120,180,240,380);
foreach($speed as $keys=>$values){
echo $keys."=>".$values."<br />";
}
?>
运行结果:
复制代码 代码如下:
0=>50
1=>120
2=>180
3=>240
4=>380
2.while循环遍历:
while循环遍历一般结合list函数,以下是实例
复制代码 代码如下:
<?PHP
$staff = array(
array("姓名","性别","年龄"),
array("小张","男",24),
array("小王","女",25),
array("小李","男",23)
);
echo "<table border=2>";
while(list($keys,$value) = each($staff)){
list($name,$sex,$age) = $value;
echo "<tr><td>$name</td><td>$sex</td><td>$age</td></tr>";
}
echo "</table>";
?>
运行结果:
姓名 性别 年龄 小张 男 24 小王 女 25 小李 男 233.for循环遍历:
复制代码 代码如下:
<?PHP
$speed = range(0,220,20);
for($i =0;$i<count($speed);$i++) {
echo $speed[$i]." ";
}
?>
运行结果:
复制代码 代码如下:
0 20 40 60 80 100 120 140 160 180 200 220
一、数组定义:
数组的定义使用 array()方式定义,可以定义空数组:
复制代码 代码如下:
<?php
$number = array(1,3,5,7,9);
//定义空数组
$result = array();
$color =array("red","blue","green");
//自定义键值
$language = (1=>"English",3=>"Chinese",5=>"Franch");
//定义二维数组
$two = array(
"color"=>array("red","blue"), //用逗号结尾
"week"=>array("Monday","Friday") //最后一句没有标点
);
?>
二、创建数组:
创建数组包含的函数有compact()、
1.compact()函数——将一个或多个变量(包含数组)转换为数组:
array compact ( mixed $varname [, mixed $... ] )
复制代码 代码如下:
<?PHP
$number = "1,3,5,7,9";
$string = "I'm PHPer";
$array = array("And","You?");
$newArray = compact("number","string","array");
print_r ($newArray);
?>
compact()函数用于将两个或多个变量转换为数组,当然也包含数组变量。其参数是变量的名称而非带有$全名。
相反的函数是extract()作用顾名思义就是将数组转换为单个的字符串,键值作为其字符串名称,数组值作为字符串的值。
运行结果:
复制代码 代码如下:
Array ( [number] => 1,3,5,7,9 [string] => I'm PHPer [array] => Array ( [0] => And [1] => You? ) )
2.array_combine()——将两个数组重组成一个数组,一个作键值一个做的值:
array array_combine ( array $keys , array $values )
复制代码 代码如下:
<?PHP
$number = array("1","3","5","7","9");
$array = array("I","Am","A","PHP","er");
$newArray = array_combine($number,$array);
print_r ($newArray);
?>
array_combine函数不多说了,谁看了都明白
运行结果:
Array ( [1] => I [3] => Am [5] => A [7] => PHP [9] => er )
3.range()函数——创建指定范围的数组:
不多说了,直接上实例——
复制代码 代码如下:
<?PHP
$array1 = range(0,100,10);//0为起始值,100为结束值,10为步进值(默认步进值为1).
print_r($array1);
echo"<br />";
$array2 = range("A","Z");
print_r($array2);
echo "<br />";
$array3 = range("z","a");
print_r($array3);
?>
range()函数的默认步进值是1!
运行结果:
复制代码 代码如下:
Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 [6] => 60 [7] => 70 [8] => 80 [9] => 90 [10] => 100 )
Array ( [0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6] => G [7] => H [8] => I [9] => J [10] => K [11] => L [12] => M [13] => N [14] => O [15] => P [16] => Q [17] => R [18] => S [19] => T [20] => U [21] => V [22] => W [23] => X [24] => Y [25] => Z )
Array ( [0] => z [1] => y [2] => x [3] => w [4] => v [5] => u [6] => t [7] => s [8] => r [9] => q [10] => p [11] => o [12] => n [13] => m [14] => l [15] => k [16] => j [17] => i [18] => h [19] => g [20] => f [21] => e [22] => d [23] => c [24] => b [25] => a )
4.array_fill()函数——填充数组函数:
复制代码 代码如下:
<?PHP
$array = range(1,10);
$fillarray = range("a","d");
$arrayFilled = array_fill(0,5,$fillarray);//这里的$fillarray可以是字符串,如"test".
echo "<pre>";
print_r ($arrayFilled);
echo "</pre>";
$keys = array("string","2",9,"SDK","PK");
$array2 = array_fill_keys($keys,"testing");
echo "<pre>";
print_r ($array2);
echo "</pre>";
?>
运行结果:
复制代码 代码如下:
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[2] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[3] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[4] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
)
Array
(
[string] => testing
[2] => testing
[9] => testing
[SDK] => testing
[PK] => testing
)
二、数组的遍历:
1.foreach遍历:
foreach (array_expression as $value){}
foreach (array_expression as $key => $value){}
闲话少说,上实例:
复制代码 代码如下:
<?PHP
$speed = array(50,120,180,240,380);
foreach($speed as $keys=>$values){
echo $keys."=>".$values."<br />";
}
?>
运行结果:
复制代码 代码如下:
0=>50
1=>120
2=>180
3=>240
4=>380
2.while循环遍历:
while循环遍历一般结合list函数,以下是实例
复制代码 代码如下:
<?PHP
$staff = array(
array("姓名","性别","年龄"),
array("小张","男",24),
array("小王","女",25),
array("小李","男",23)
);
echo "<table border=2>";
while(list($keys,$value) = each($staff)){
list($name,$sex,$age) = $value;
echo "<tr><td>$name</td><td>$sex</td><td>$age</td></tr>";
}
echo "</table>";
?>
运行结果:
姓名 性别 年龄 小张 男 24 小王 女 25 小李 男 233.for循环遍历:
复制代码 代码如下:
<?PHP
$speed = range(0,220,20);
for($i =0;$i<count($speed);$i++) {
echo $speed[$i]." ";
}
?>
运行结果:
复制代码 代码如下:
0 20 40 60 80 100 120 140 160 180 200 220
标签:
数组
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年11月14日
2024年11月14日
- 网友热议IGN为《马路RPG》打5分:要是多元化就能9分了
- 群星《从21世纪安全撤离 电影原声音乐专辑》[320K/MP3][191.19MB]
- 群星《从21世纪安全撤离 电影原声音乐专辑》[FLAC/分轨][592.38MB]
- 群星《奔赴!万人现场 第5期》[320K/MP3][106.99MB]
- 许魏洲.2024-CrossFever交互热爱【智慧大狗】【FLAC分轨】
- BEYOND.1993-FINAL.LIVE.WITH家驹【华纳】【WAV+CUE】
- 梅艳芳.1994-是这样的(金碟版)【华星】【WAV+CUE】
- 张学友《真情流露》HQ+S纯银深度[低速原抓WAV+CUE]
- 江志丰2012-七天[豪记][WAV+CUE]
- 黑鸭子2003《聆听柔情HQCD》[日本版][WAV+CUE]
- 群星《奔赴!万人现场 第5期》[FLAC/分轨][587.07MB]
- 关大洲《国家宝藏 第四季原声音乐 关大洲作品》[320K/MP3][109.49MB]
- 关大洲《国家宝藏 第四季原声音乐 关大洲作品》[FLAC/分轨][527.23MB]
- LOL双城之战大乱斗什么时候更新 双城大乱斗上线更新时间介绍
- s14全球总决赛冠军皮肤有什么 2024T1冠军皮肤选择一览