標簽:
數組:
特點:1.可以存儲任意類型的數據 2.可以不連續 3.可以是索引的,也可以是關聯的。
定義數組的第一種方式(定義簡單的索引數組)
$attr=array(1,2,3);
定義數組的第二種方式(賦值定義)
$attr[]=1;
$attr[]=2;
$attr[]=3;
定義數組的第三種方式(定義一個關聯的數組)
$attr=array
(
“one”=>”hello”,
“two”=>100,
“three”=>10.9
)
數組取值(根據索引取值)
$attr=array(1,2,3);
$attr[0];
(根據k取值)
$attr=array
(
“one”=>”hello”,
“two”=>100,
“three”=>10.9
)
$attr[“one”]
遍歷數組
for循環 適用于索引數組
$attr=array(1,2,3);
For($i=0;$i<count($attr),$i++)
{
$attr[$i]
}
foreach遍歷 適用于所有數組
$attr=array
(
“one”=>”hello”,
“two”=>100,
“three”=>10.9
)
Foreach($attr as $a)
{
$a.”<br>”
}
foreach第二種形式:可以把k和值全部取出來
Foreach($attr as $a=>$v)
{
“$a--$v<br>”
}
適用于each()和list()結合遍歷數組
$attr=array
(
“one”=>”hello”,
“two”=>100,
“three”=>10.9
)
Each($attr) 返回數組里面的當前元素的詳細內容
將右側數組里面的每個元素分別賦值給list()的參數列表,注意右側數組必須包含索引。
$attr=array(1,2,3);
List($a,$b,$c)=$attr
while遍歷 適用于each和list結合數組
$attr=array
(
“one”=>”hello”,
“two”=>100,
“three”=>10.9
)
while(list($a,$b)=each($attr))
{
“$a--$b<br>”
}
標簽: