(如有任何不清楚或不懂之處,歡迎到我的居家(G+) 討論 http://gplus.to/gplus2 討論喔)
簡介
在php中的陣列 array ,可以看成 Map, 也就是 a set of (key,value) pairs
沒有特定指字 key 的方式宣告的 array 叫數值陣列,也就是像 C 一般在用的陣列。
另外有宣告 key 的,用法就像 C++ 的 Maps 一般。
以下我們介紹有關於 陣列的 宣告方式、存取方式 及 相關基本函數。
當然最後也有講到陣列如何排序。
一、陣列宣告的方式
(請見sample code
example-4-01.php
example-4-02.php
)
// An array called $vegetables with string keys
$vegetables[‘corn’] = ‘yellow’;
$vegetables[‘beet’] = ‘red’;
$vegetables[‘carrot’] = ‘orange’;
// An array called $dinner with numeric keys
$dinner[0] = ‘Sweet Corn and Asparagus’;
$dinner[1] = ‘Lemon Chicken’;
$dinner[2] = ‘Braised Bamboo Fungus’;
$dinner[] = ‘GoGo’; //add by milo but not in example
// An array called $computers with numeric and string keys
$computers[‘trs-80’] = ‘Radio Shack’;
$computers[2600] = ‘Atari’;
$computers[‘Adam’] = ‘Coleco’;
上面等價的含意為
$vegetables = array(‘corn’ => ‘yellow’,
‘beet’ => ‘red’,
‘carrot’ => ‘orange’);
$dinner = array( 0 => ‘Sweet Corn and Asparagus’,
1 => ‘Lemon Chicken’,
2 => ‘Braised Bamboo Fungus’,
3 => ‘GoGo’ //add by milo but not in example
);
$computers = array(‘trs-80’ => ‘Radio Shack’,
2600 => ‘Atari’,
‘Adam’ => ‘Coleco’);
二、陣列存取方式
(接續著上面已宣告的資料,我們可以由以下的存取方式)
(1) 各別存取
$var = $dinner[0];
$var = $dinner[1];
$var = $vegetables[‘carrot’];
(2) 以 for 作存取
for ($i = 0, $num_dishes = count($dinner); $i < $num_dishes; $i++)
{
$var = $dinner[$i];
}
(3) 以 foreach 作存取
foreach ($dinner as $dish)
{
$var = $dish;
}
foreach ($vegetables as $dish => $color)
{
$var_key = $dish;
$var_value = $color;
}
(4) foreach 內元素順序,與宣告順序有關
$letters[0] = ‘A’;
$letters[1] = ‘B’;
$letters[3] = ‘D’;
$letters[2] = ‘C’;
foreach ($letters as $letter) {
print "$letter";
}
這段程式將印出 A B D C
三、php 為陣列瑅供的基本函數
(1) 用 array_key_exists() 檢查 array 是否存在 key
e.g.
1. array_key_exists(‘corn’, $vegetables) 回傳 true
2. array_key_exists(‘corn2’, $vegetables) 回傳 false
(2) 用 in_array() 檢查 array 是否存在 value
e.g.
1. in_array(‘yellow’, $vegetables) 回傳 true
2. in_array(‘yellow2’, $vegetables) 回傳 false
(3) 用 array_search() 尋找第一筆含有 value為V 的 pair, 並回傳其key
sample code.
//return the first food in vegetables that color is yellow
$food = array_search(‘yellow’, $vegetables);
if ($food)
{
print "The color of $food is yellow";
}
(4) 用 implode() 將陣列轉換成字串
e.g. implode(" – ",$vegetables)
回傳字串 "yellow – red – orange"
(5) 用 explode() 將字串轉換成陣列
e.g. explode(‘ – ‘, "large – middle – small")
回傳陣列 array(‘large’, ‘middle’, ‘small’)
四、陣列排序
在php中的陣列 array ,可以看成 Map, 也就是 a set of (key,value) pairs
一般是使用 asort() 或 ksort() 作排序。若是要sort數值陣列的話,由 sort()完成.
無論是 asort() 或 ksort(),在排序過程中,排列使的swap 都是整個 pair 在swap 的
差別在於 asort()參考 pair 中的 value 作排序
而 ksort() 則是參考 pair 中的 key 作排序
sort()的話,專用來排序"數字值列",若不是數值陣列就會被轉成數值陣列,這點要注意.
把asort,ksort看成順向排序,則它的逆向排序分別為 arsort() 跟 krsort()
而 rsort() 則是 sort() 的逆向排序
以下是sort(), asort() 及 ksort(), rsort(), arsort(), krsort() 的排序範例
(1) sort()
Before Sorting
$sort_array_a = {
corn => yellow,
beet => red,
carrot => orange,
}
$sort_array_b = {
0 => Sweet Corn and Asparagus,
1 => Lemon Chicken,
2 => Braised Bamboo Fungus,
3 => GoGo,
}
After Sorting
$sort_array_a = {
0 => orange,
1 => red,
2 => yellow,
}
$sort_array_b = {
0 => Braised Bamboo Fungus,
1 => GoGo,
2 => Lemon Chicken,
3 => Sweet Corn and Asparagus,
}
(2) asort()
Before Sorting
$asort_array_a = {
corn => yellow,
beet => red,
carrot => orange,
}
$asort_array_b = {
0 => Sweet Corn and Asparagus,
1 => Lemon Chicken,
2 => Braised Bamboo Fungus,
3 => GoGo,
}
After Sorting
$asort_array_b = {
carrot => orange,
beet => red,
corn => yellow,
}
$asort_array_b = {
2 => Braised Bamboo Fungus,
3 => GoGo,
1 => Lemon Chicken,
0 => Sweet Corn and Asparagus,
}
(3) ksort()
Before Sorting
$ksort_array_a = {
corn => yellow,
beet => red,
carrot => orange,
}
$ksort_array_b = {
0 => Sweet Corn and Asparagus,
1 => Lemon Chicken,
2 => Braised Bamboo Fungus,
3 => GoGo,
}
After Sorting
$ksort_array_a = {
beet => red,
carrot => orange,
corn => yellow,
}
$ksort_array_b = {
0 => Sweet Corn and Asparagus,
1 => Lemon Chicken,
2 => Braised Bamboo Fungus,
3 => GoGo,
}
(4) rsort()
Before Sorting
$rsort_array_a = {
corn => yellow,
beet => red,
carrot => orange,
}
$rsort_array_b = {
0 => Sweet Corn and Asparagus,
1 => Lemon Chicken,
2 => Braised Bamboo Fungus,
3 => GoGo,
}
After Sorting
$rsort_array_a = {
0 => yellow,
1 => red,
2 => orange,
}
$rsort_array_b = {
0 => Sweet Corn and Asparagus,
1 => Lemon Chicken,
2 => GoGo,
3 => Braised Bamboo Fungus,
}
(5) arsort()
Before Sorting
$arsort_array_a = {
corn => yellow,
beet => red,
carrot => orange,
}
$arsort_array_b = {
0 => Sweet Corn and Asparagus,
1 => Lemon Chicken,
2 => Braised Bamboo Fungus,
3 => GoGo,
}
After Sorting
$arsort_array_a = {
corn => yellow,
beet => red,
carrot => orange,
}
$asort_array_b = {
2 => Braised Bamboo Fungus,
3 => GoGo,
1 => Lemon Chicken,
0 => Sweet Corn and Asparagus,
}
(6) krsort()
Before Sorting
$krsort_array_a = {
corn => yellow,
beet => red,
carrot => orange,
}
$krsort_array_b = {
0 => Sweet Corn and Asparagus,
1 => Lemon Chicken,
2 => Braised Bamboo Fungus,
3 => GoGo,
}
After Sorting
$krsort_array_a = {
corn => yellow,
carrot => orange,
beet => red,
}
$krsort_array_b = {
3 => GoGo,
2 => Braised Bamboo Fungus,
1 => Lemon Chicken,
0 => Sweet Corn and Asparagus,
}
發表留言