Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP 5 陣列與自定函數 陣列應用 自定函數.

Similar presentations


Presentation on theme: "PHP 5 陣列與自定函數 陣列應用 自定函數."— Presentation transcript:

1 PHP 5 陣列與自定函數 陣列應用 自定函數

2 陣列應用 所謂的陣列就是指集合相同屬性的變數所成 的集合,可分成一維陣列、二維陣列… 一維陣列表示方式 $陣列名稱[指標]
範例:$Name[0] = "Joe" ; $Name[1] = "Ken" ; $Name[2] = "Cherry" ; $Name[3] = "Maggy";

3 陣列應用 多維陣列表示方式 設定陣列的初始值 直接設定 函數設定 二維: $陣列名稱[指標1] [指標2] = 值
三維: $陣列名稱[指標1] [指標2] [指標3] = 值 四維: $陣列名稱[指標1] [指標2] [指標3] [指標4] = 值 設定陣列的初始值 直接設定 $a[1]=20; $a[]=30;  存入陣列「最尾端」 函數設定 陣列名稱 = array(第0元素, 第1元素, 第2元素, ...)  需依序指定值 陣列名稱 = array(0 =>第0元素, 1 =>第1元素, ...)  可依index指定 (有實例)

4 陣列應用 設定二維陣列初始值 $陣列名稱 = array( 0 => array( 0 => 第0列第0行的值
1 => 第0列第1行的值 2 => 第0列第2行的值 ) , 1 => array( 0 => 第1列第0行的值, 1 => 第1列第1行的值, 2 => 第1列第2行的值 ), 以此類推 ... ) ;

5 $arr3x4[2][1] $arr3x4 = array( array("a1", "a2", "a3", "a4"),
b1, b2, b3, b4 c1, c2, c3, c4 $arr3x4[2][1] $arr3x4 = array( array("a1", "a2", "a3", "a4"), array("b1", "b2", "b3", "b4"), array("c1", "c2", "c3", "c4"));

6 <?php $arr2d = array(array(1,2,3), array(4,5,6), array(7,8,9)); for ($i=0; $i<count($arr2d); $i++) { for ($j=0; $j<count($arr2d[$i]); $j++) { echo "($i, $j) = {$arr2d[$i][$j]} <br/>"; } echo "<hr/>"; ?>

7 Associative Array 可使用文字指標如下: 1: <?php
2: $a=array( "Mon" => "星期一", 3: "Tue" => "星期二", 4: "Wed" => "星期三"); 5: foreach ($a as $k => $v) 6: echo "A[ $k ] = $v <br>"; 7: ?>

8 Associative Array (Example)
<?php $prod["p001"]= array("Sony 16G隨身碟", 1200); $prod["p002"]= array("Asus g基地台", 3100); $prod["p003"]= array("Acer 150G外接式硬碟", 2780); $prod["p004"]= array("Logitech 無線光學滑鼠", 1340); $prod["p005"]= array("iPhone 5S", 22100); echo "<ul>"; foreach ($prod as $pid => $pInfo) { echo "<li>產品: $pInfo[0], 售價: NT\$$pInfo[1], "; echo "<a href=\"addCart.php?id=$pid\">放入購物車</a></li>"; } echo "</ul>"; ?>

9 陣列存取範例 count函數: count()函數會傳回由 [陣列名稱] 所指定的陣列元素的個數。 <?php
$s = array (0,15,10,20,17,3,8,10,15,12) ; $cnt=count($s); for ($i=1 ; $i<$cnt; $i++){ echo "銷售員$i : " ; $s[0]+=$s[$i] ; $p=$s[$i]*20; echo "<img src=\"line.png\" width=\"$p\" height=\"10\">"; echo " $s[$i]<br/>\n" ; } echo "總量: $s[0]" ; ?>

10 array相關函數 http://www.w3schools.com/PHP/php_ref_array.asp Function
Description array() Creates an array range() Creates an array containing a range of elements array_merge() Merges one or more arrays into one array array_search() Searches an array for a given value and returns the key in_array() Checks if a specified value exists in an array array_key_exists() Checks if the specified key exists in the array array_unshift() Adds one or more elements to the beginning of an array array_shift() Removes the first element from an array, and returns the removed value. array_slice() Returns selected parts of an array array_splice() Removes and replaces specified elements of an array array_pop() Deletes the last element of an array array_push() Inserts one or more elements to the end of an array array_rand() Returns one or more random keys from an array

11 Function Description array_sum() Returns the sum of the values in an array array_product() Calculates the product of the values in an array array_unique() Removes duplicate values from an array array_values() Returns all the values of an array array_keys() Returns all the keys of an array count() Counts elements in an array, or properties in an object sizeof() Alias of count() list() Assigns variables as if they were an array each() Returns the current key and value pair from an array

12 Function Description current() Returns the current element in an array pos() Alias of current() key() Fetches a key from an array next() Advance the internal array pointer of an array prev() Rewinds the internal array pointer reset() Sets the internal pointer of an array to its first element end() Sets the internal pointer of an array to its last element

13 Array Sorting Function Description sort() Sorts an array rsort()
Sorts an array in reverse order asort() Sorts an array and maintain index association arsort() Sorts an array in reverse order and maintain index association ksort() Sorts an array by key krsort() Sorts an array by key in reverse order natsort() Sorts an array using a "natural order" algorithm natcasesort() Sorts an array using a case insensitive "natural order" algorithm

14 自定函數 建立函數 參數的傳遞 預設參數值

15 建立函數 自定函數的格式如下 function 自定函數名稱 ($arg1, $arg2, ... $argn) { 函數敘述 ;
return 函數返回值 ; }

16 建立函數 自定函數範例 1: <?php 2: function Pay($price, $amount) {
3: echo "價格 : $price <br> 數量 : $amount <br>\n"; 4: echo "金額: ".$price * $amount."<p>" ; 5: } 6: ?> 7: <html> 8: <title>自定函數</title> 9: <body> 10: <?php 11: $Price=90 ; 12: $amount=100 ; 13: Pay($Price, $amount) ; //第一次呼叫函數 14: $Price=60 ; 15: $amount=50 ; 16: Pay($Price, $amount) ; //第二次呼叫函數 17: ?> 18: </body> 19: </html>

17 函數有多個回傳值 一般自定函數都只能有「單一回傳值」, 而PHP 中可利用list 函數接收「多個回傳值。 範例: 1: <?php
2: function message() { 3: echo "函數回傳三個數值.<p>"; 4: return array("Hello", "My", "friend"); 5: } 6: ?> 7: <html> 8: <title>多個回傳值</title> 9: <body> 10: <?php 11: list($msg1, $msg2, $msg3) = message(); 12: echo "主程式接收多個訊息如下<br>"; 13: echo "$msg1 $msg2 $msg3"; 14: ?> 15: </body> 16: </html>

18 Example <?php function cmp($a,$b) { if ($a<=$b)
return array($a, $b); else return array($b, $a); } list($s,$g) = cmp(5,2); echo "$s is smaller than $g"; ?>

19 自定函數-參數的傳遞 傳址呼叫 (Call By Value) 傳值呼叫的方式是由主程式傳遞變數的值給自 訂函數的參數
傳值呼叫 (Call By Reference) 傳址呼叫是傳入變數的位址而不是值, 函數參數是接 受主程式傳來的一個位址。因此參數會指向原變數 由於 PHP 5 的預設值不予許使用 call by reference, 需修改 php.ini之 allow_call_time_pass_reference = On 設定為 on才可以使用.

20 自定函數-參數的傳遞 <?php function Pay($Price, $amount, $Total) {
echo "=========== in Function Pay ======== <br/>" ; echo "Price : $Price <br/> Amount : $amount <br/>\n"; $Total=$Price * $amount ; echo "Total in Function Pay : $Total <p>\n" ; } ?> <html> <title>傳值呼叫</title> <body> $Price=90 ; $amount=100 ; $Total=0 ; Pay($Price, $amount, $Total) ; echo "============== in Main ===========<br/>" ; echo "Total in main : $Total<br/>" ; </body> </html>

21 自定函數-參數的傳遞 傳址呼叫 (Call By Reference)-Type 1
放 function 定義的參數: ‘&’ 寫在函數參數列。 1: <?php 2: function Pay($Price, $amount, &$Total) { 3: echo "=========== in Function Pay ========== <br>" ; 4: echo "Price : $Price <br> Amount : $amount <br>\n"; 5: $Total=$Price * $amount ; 6: echo "Total in Function Pay : $Total <p>\n" ; 7: } 8: ?> 9: <html> 10: <title>傳址呼叫</title> 11: <body> 12: <?php 13: $Price=90 ; 14: $amount=100 ; 15: $Total=0 ; 16: Pay($Price, $amount, $Total) ; 17: echo "============== in Main ==============<br>" ; 18: echo "Price : $Price <br> Amount : $amount <br>\n"; 19: echo "Total in main : $Total<br>" ; 20: ?> 21: </body> 22: </html>

22 自定函數-參數的傳遞 傳址呼叫 (Call By Reference)-Type 2 呼叫函數時決定如何傳遞參數:傳值或傳址呼叫。
1: <?php 2: function Pay($price, $amount, $total) { 3: echo "Price : $price Amount : $amount <br>\n"; 4: $total=$price * $amount ; 5: echo "Total in Function Pay : $total <br>\n" ; 6: echo "<p>\n" ; 7: } 8: ?> 9: <html> 10: <title>選擇傳遞方式</title> 11: <body> 12: <?php 13: $Price=90 ; 14: $amount=100 ; 15: $Total=0 ; 16: Pay($Price, $amount, $Total) ; 17: echo "Total in main : $Total <== 傳值呼叫<hr><p>\n" ; 18: Pay($Price, $amount, &$Total) ; 19: echo "Total in main : $Total <== 傳址呼叫<hr>" ; 20: ?> 21: </body> 22: </html>

23 預設參數值 預設參數值 注意事項: 在自訂函數撰寫時可以事先給予參數一個預設值, 當主 程式呼叫函數時有二種選擇。
主程式有傳值給函數: 則函數中參數值即以主程式傳 入的值代入。 主程式沒有傳值給函數: 則以函數將以函數定義的預 設值帶入參數。 預設參數值 注意事項: 第一點, 預設值必須是一個常數, 不可以是變數。 第二點, 預設值的參數必須是在參數列的右邊開始, 由右向左延 伸。這一點很容易理解, 如果有一函數如下  錯誤示範 function test ($a=10, $B)

24 預設參數值 範例 1: <?php 2: function Pay($price, $amount, $discount=0.8) {
3: echo "價格 : $price 元<br>" ; 4: echo "數量 : $amount 個<br>" ; 5: $showcnt=$discount*10 ; 6: echo "折扣 : $showcnt 折<br> " ; 7: return $price*$amount*$discount ; 8: } 9: ?> 10: <html> 11: <title>預設參數</title> 12: <body> 13: <?php 14: $Price=100 ; 15: $amount=8 ; 16: echo "特別折扣:<br>" ; 17: $Discount=0.5 ; 18: $cost=Pay($Price, $amount, $Discount) ; //傳入$Discount參數 19: echo "總價 : $cost 元<p>\n" ; 20: echo "一般折扣:<br>" ; 21: $cost=Pay($Price, $amount) ;//未傳入$Discount, 以預設值帶入 22: echo "總價 : $cost 元" ; 23: ?> 24: </body> 25: </html>

25 可變長度參數 一種參數傳輸技巧, 可不需先行宣告自訂函數的 參數值 配合可變長度參數擷取參數時所使用的函數.
func_num_args() 會傳回傳入參數的個數. func_get_ars(n) 會傳回第 n 個參數, 參數是由第 0 個開 始計算. 如果 n 值大於參數個數則會傳回 False. func_get_args() 將所有參數以陣列方式傳出.

26 可變長度參數範例 1: <?php 2: function var_len(){ 3: $n=func_num_args();
4: echo "總共傳入".$n."個參數<br/>"; 5: echo "參數內容如下<br/>"; 6: $op=func_get_args(); 7: for ($i=0; $i<$n; $i++){ 8: echo "- $op[$i] <br/>"; 9: } 10: } 11: ?> 12: <html> 13: <title>可變長度參數</title> 14: <body> 15: <?php 16: echo "2個參數 <br/>"; 17: var_len(10,20); 18: echo "<p>"; 19: echo "3個參數 <br/>"; 20: var_len("Hello","My","Friend"); 21: ?> 22: </body> 23: </html>


Download ppt "PHP 5 陣列與自定函數 陣列應用 自定函數."

Similar presentations


Ads by Google