Download presentation
Presentation is loading. Please wait.
1
進階 WWW 程式設計 -- PHP Array 靜宜大學資訊管理學系 蔡奇偉副教授 2003-2005
2019/5/21 進階 WWW 程式設計 PHP Array 靜宜大學資訊管理學系 蔡奇偉副教授 靜宜大學資管系 蔡奇偉編撰 版權所有
2
內容大綱 陣列的種類 陣列的產生方式 讀取陣列元素值 字串中展開陣列變數值 改變陣列元素值 foreach 迴圈指令 list() 指令
插入、移除、與置換元素
3
陣列的種類 PHP 的陣列可用來儲存一組相同或不同型態的資料。PHP 提供下列兩種不同存取方式的陣列:
索引陣列(indexed array) 用一個整數索引來存取陣列中的值。 40 25 3.14 ‘Hello’ 1 2 99 indices 對照陣列(associative array) 用一個字串型態的鍵(key)來存取陣列中的對照值。 ‘lastname’ ‘陳’ ‘firstname’ ‘水扁’ ‘sex’ ‘male’ keys values
4
陣列的產生方式 一、使用指定敘述 40 50 ?? 20 10 1 2 3 4 $myArray NULL
1 2 3 4 $myArray NULL $myArray[0] = 40; $myArray[1] = 50; $myArray[3] = 20; $myArray[4] = 10; ‘lastname’ ‘蘇’ ‘firstname’ ‘東坡’ ‘sex’ ‘male’ keys values $myForm $myForm[‘lastname’] = ‘蘇’; $myForm [‘firstname’] = ‘東坡’; $myForm [‘sex’] = ‘male’;
5
二、使用 PHP 的陣列建構函式: array()
# 建立索引陣列 $myArray = array(40, 50, NULL, 20, 10); 40 50 NULL 20 10 1 2 3 4 $myArray ‘lastname’ ‘蘇’ ‘firstname’ ‘東坡’ ‘sex’ ‘male’ keys values $myForm # 建立對照陣列 $myForm = array(‘lastname’ => ‘蘇’, ‘firstname’ => ‘東坡’, ‘sex’ => ‘male’);
6
判別變數是否是陣列型態 bool is_array ( mixed var ) 範例
若參數 var 是陣列,則傳回 true,否則傳回 false。 範例 // check for the array if (!is_array($valArray) { // action if not an array echo "$valArray"; } else { foreach ($valArray as $key => $val) { // action if it is an array echo "valArray[$key] = " . $val; }
7
讀取陣列元素值 範例 範例 索引陣列可用以下格式藉索引(index)來讀取其對應的元素值: $array_name[index]
$myArray = array(40, 50, NULL, 20, 10); $value = $myArray[3]; # $value is 20 對照陣列可用以下格式藉對照鍵(key)來讀取其對照值: $array_name[“key”] $myForm = array(‘lastname’=>‘蘇’, ‘firstname’=>‘東坡’, ‘sex’ => ‘male’); $value = $myForm[‘firstname’]; # $value is ‘東坡’ 範例 範例
8
字串中展開陣列變數值 範例 $myArray = array(40, 50, NULL, 20, 10);
$myForm = array('lastname' => '蘇', 'firstname' => '東坡', 'sex' => 'male'); print "myArray = $myArray\n"; print "myForm = $myForm\n"; print "myArray[3] = $myArray[3]\n"; print "myForm['firstname'] = $myForm['firstname']\n"; # wrong print "myForm['firstname'] = $myForm[firstname]\n"; # right 執行結果: myArray = Array myForm = Array myArray[3] = 20 myForm['firstname'] = myForm['firstname'] = 東坡
9
改變陣列元素值 範例 範例 索引陣列可用以下格式藉索引(index)來改變其對應的元素值:
$array_name[index] = expression $myArray = array(40, 50, NULL, 20, 10); $myArray[3] = 99; # $myArray[3] 的值變成 99 對照陣列可用以下格式藉對照鍵(key)來改變其對照值: $array_name[“key”] = expression $myForm = array('lastname' => '蘇', 'firstname' => '東坡', 'sex' => 'male'); $myForm['firstname'] = '軾'; # $myForm[‘firstname’] 的值變成 '軾' 範例 範例
10
取得陣列的元素個數 範例 函式 count() 或 sizeof() 都可用來取得陣列的元素個數。 $a = array(1, 3, 5);
$result = count ($a); // $result == 3 $b[0] = 7; $b[5] = 9; $b[10] = 11; $result = count ($b); // $result == 3; $c = array(); $result = count ($c); // $result == 0; $d = 99; $result = count ($d); // $result == 1;
11
foreach 迴圈指令 foreach(array_expression as $value) statement foreach(array_expression as $key => $value) statement 第一個格式常用於索引陣列、第二個格式常用於對照陣列。 範例 $a = array (1, 2, 3, 17); foreach ($a as $v) { print "Current value of \$a: $v.\n"; } 輸出結果: Current value of $a: 1. Current value of $a: 2. Current value of $a: 3. Current value of $a: 17.
12
範例 $a = array (1, 2, 3, 17); $i = 0; /* for illustrative purposes only */ foreach($a as $v) { print "\$a[$i] => $v.\n"; $i++; } 輸出結果: $a[0] => 1. $a[1] => 2. $a[2] => 3. $a[3] => 17.
13
範例 $a = array ('one' => 1, 'two' => 2, 'three' => 3, 'seventeen' => 17 ); foreach($a as $k => $v) { print "\$a[$k] => $v.\n"; } 輸出結果: $a[one] => 1. $a[two] => 2. $a[three] => 3. $a[seventeen] => 17.
14
範例 foreach(array(1, 2, 3, 4, 5) as $v) { print "$v\n"; } 輸出結果: 1 2 3 4
15
list() 指令 範例 list() 指令用來把索引陣列的元素存入一組變數。
$info = array('coffee', 'brown', 'caffeine'); // Listing all the variables list($drink, $color, $power) = $info; print "$drink is $color and $power makes it special.\n"; // Listing some of them list($drink, , $power) = $info; print "$drink has $power.\n"; // Or let's skip to only the third one list( , , $power) = $info; print "I need $power!\n";
16
Using list() with array indices
範例 Using list() with array indices $info = array('coffee', 'brown', 'caffeine'); list($a[0], $a[1], $a[2]) = $info; var_dump($a); 輸出結果: array(3) { [2]=> string(8) "caffeine" [1]=> string(5) "brown" [0]=> string(6) "coffee" }
17
移除陣列與陣列元素 範例 函式 unset() 可以用來移除陣列或陣列元素。
$a = array(1 => 'one', 2 => 'two', 3 => 'three'); unset($a[2]); /* will produce an array that would have been defined as $a = array(1 => 'one', 3 => 'three'); and NOT $a = array(1 => 'one', 2 =>'three'); */ $b = array_values($a); // Now b is array(1 => 'one', 2 =>'three') unset($a); // now a is undefined
18
插入、移除、與置換元素 在陣列尾端加入新元素 在陣列首/尾端補上同值的新元素 array_fill() 函式
array_shift() 函式 array_unshift() 函式
19
在陣列尾端加入新元素 註 我們可以用下列的方式在陣列尾端加入一個新元素: $a = array(1, 2, 3, 4);
$a[] = 5; // a now is (1, 2, 3, 4, 5) 此外,函式 array_push() 也可用來在陣列尾端加入一個或多個元素, 如: array_push ($a, 5, 6, 7); // a now is (1, 2, ,3, 4, 5, 6, 7) 註 array_push() 只能用於經過初值設定的陣列上。 array_pop() 可用來移除陣列尾端的元素。 用 array_push() 和 array_pop() 可模擬 stack 的操作。
20
在陣列首/尾端補上同值的新元素 array array_pad ( array input, int pad_size,
mixed pad_value ) array_pad() returns a copy of the input padded to size specified by pad_size with value pad_value. If pad_size is positive then the array is padded on the right, if it's negative then on the left. If the absolute value of pad_size is less than or equal to the length of the input then no padding takes place. 範例 $input = array (12, 10, 9); $result = array_pad ($input, 5, 0); // result is array (12, 10, 9, 0, 0) $result = array_pad ($input, -4, -1); // result is array (-1, 12, 10, 9) $result = array_pad ($input, 2, "noop"); // not padded
21
array_fill 函式 array array_fill ( int start_index, int num, mixed value ) array_fill() fills an array with num entries of the value of the value parameter, keys starting at the start_index parameter. 範例 $a = array_fill(5, 3, 'banana'); print_r($a); 輸出的結果: Array ( [5] => banana [6] => banana [7] => banana )
22
array_shift() 函式 mixed array_shift ( array array ) 範例
array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched. If array is empty (or is not an array), NULL will be returned. 範例 $a = array ("orange", "banana", "apple", "raspberry"); $fruit = array_shift ($a); // $fruit == "orange“ // $a becomes ("banana", "apple", "raspberry")
23
array_unshift() 函式 int array_unshift ( array array, mixed var [, mixed ... ] ) array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be touched. Returns the new number of elements in the array. 範例 $a = array ("orange", "banana"); array_unshift ($a, "apple", "raspberry"); // $a becomes ("apple", "raspberry“, "orange", "banana" )
24
array_splice() 函式 array array_splice ( array input, int offset
[, int length [, array replacement ]] ) array_splice() removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied. It returns an array containing the extracted elements. If offset is positive then the start of removed portion is at that offset from the beginning of the input array. If offset is negative then it starts that far from the end of the input array.
25
進階 WWW 程式設計 -- PHP Array 2019/5/21 If length is omitted, removes everything from offset to the end of the array. If length is specified and is positive, then that many elements will be removed. If length is specified and is negative then the end of the removed portion will be that many elements from the end of the array. Tip: to remove everything from offset to the end of the array when replacement is also specified, use count($input) for length. If replacement array is specified, then the removed elements are replaced with elements from this array. If offset and length are such that nothing is removed, then the elements from the replacement array are inserted in the place specified by the offset. Tip: if the replacement is just one element it is not necessary to put array() around it, unless the element is an array itself. 靜宜大學資管系 蔡奇偉編撰 版權所有
Similar presentations