三种数组类型:
1.数值数组:带有数字id键的数组
2.关联数组:数组中的每个id键关联一个值
3.多维数组:包含一个或多个数组的数组
数值数组
数值数组存储的每个元素有带有一个数字id键(索引?)
创建数值数组的两种方法:
$names = array("Peter","Quagmire","Joe");
$names[0] = "Peter";$names[1] = "Quagmire";$names[2] = "Joe";
以上代码都可以创建数值数组,下面是在脚本中使用这些id
输出结果:
Quagmire and Joe are Peter's neighbors
关联数组
通过关联数组,我们可以把值作为键,并向它们赋值(类似于C#中的字典集合)
下面为关联数组的创建方法
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
$ages['Peter'] = "32";$ages['Quagmire'] = "30";$ages['Joe'] = "34";
在脚本中使用
输出结果
Peter is 32 years old.
多维数组
在多维数组中,每个元素的值都是一个数组,以次类推
$families = array( "Griffin"=>array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ));
输出一个值
echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?";
输出结果
Is Megan a part of the Griffin family?