數組:
特點: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>”
}
-
指針遍歷
$attr=arry(
“one”=>”aaaa”,
“two”=>”bbbb”,
“three”=>”cccc”
);
Current($attr) 取當前元素的values值
Key($attr)取當年元素的key
Next($attr) 將數組里的指針指向下一個(向下移)
Prev($attr) 將數組里的指針指向上一個(向上移)
End($attr)將指針指向最后一個
Reset($attr)將指針指向第一個
指針遍歷
Do(current ($attr))
While(next($attr))
{
Echo current ($attr)
}
