php学习之旅 (4) 数组
1、数组种类
数值索引数组:具有数值索引的数组
相关联数组:每个ID键关联一个值
多维数组:包含一个或多个数组的数组
2、数值索引数组
//第一种初始化方法
$cars=array("Saab","Volvo","BMW","Toyota");
//第二种初始化方法
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
用法举例
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";//输出Saab and Volvo are Swedish cars.
?>
3、关联数组
//第一种初始化方法
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
//第二种初始化方法
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
用法举例
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old."; //输出Peter is 32 years old.
?>
3、多维数组
$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?
分享我的文章
程序员赚钱之路探索