1 / 17

PHP 網頁程式設計 chap 6

PHP 網頁程式設計 chap 6. 指導老師 : 黃鈴玲 學生 : 葉冠良. outline. PHP 陣列宣告 一維陣列 結合陣列 陣列收尋與排序 字串函數. PHP 陣列宣告. 陣列的元素儲存一種對應關係 :Keys 和 Values $name = array("Jollen", "Paul", "Ketty"); 等於 $name[0] = "Jollen"; $name[1] = "Paul"; $name[2] = "Ketty";. PHP 陣列宣告. $row0 = array(54, 68);

shanae
Download Presentation

PHP 網頁程式設計 chap 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP 網頁程式設計chap 6 指導老師:黃鈴玲 學生:葉冠良

  2. outline • PHP 陣列宣告 • 一維陣列 • 結合陣列 • 陣列收尋與排序 • 字串函數

  3. PHP 陣列宣告 • 陣列的元素儲存一種對應關係:Keys和Values $name = array("Jollen", "Paul", "Ketty"); 等於 $name[0] = "Jollen"; $name[1] = "Paul";$name[2] = "Ketty";

  4. PHP 陣列宣告 • $row0 = array(54, 68); • $row1 = array(67, 78); • $row2 = array(89, 93);//指定一維陣列的元素 • 建立二維陣列 • $scores = array($row0, $row1, $row2);

  5. PHP 陣列宣告 • $fruits = array(   "fruits"=>array("a"=>"orange","b"=>"banana“ ,"c"=>"apple"),"numbers" => array(1, 2, 3, 4, 5, 6) ); • 等於 • $fruits["fruits"]["a"] = "orange";$fruits["fruits"]["b"] = "banana";$fruits["fruits"]["c"] = "apple";$fruits["numbers"][0] = 1;$fruits["numbers"][1] = 2;$fruits["numbers"][2] = 3;$fruits["numbers"][3] = 4;$fruits["numbers"][4] = 5;$fruits["numbers"][5] = 6;

  6. outline • PHP 陣列宣告 • 一維陣列 • 結合陣列 • 陣列收尋與排序 • 字串函數

  7. // 指定陣列元素 $tips = array(300, 240, 100, 400); $names[] = "Joe"; $names[] = "Mary"; $names[] = "Jane"; // 計算小費總和 $tips[2] = 150; // 更改陣列值 $total = 0; for ($i = 0; $i < count($tips); $i++) { echo "$i : $tips[$i] "; $total += $tips[$i]; } echo "<br>小費總和: ". $total. "<br>"; // 顯示名稱清單 for ($i = 0; $i < count($names); $i++) echo "$i : $names[$i] "; 一維陣列範例 6.1.2

  8. // 指定陣列元素 $tips = array(300, 240, 100, 400); $names = array("Joe", "Mary", "Jane"); // 計算小費總和 $total = 0; Foreach ($tips as $element) { echo "$element "; $total += $element; } echo "<br>小費總和: ". $total. "<br>"; // 顯示小費 print_r($tips); echo "<br>"; // 顯示名稱清單 print_r($names); Foreach 6.1.3

  9. 索引陣列函數應用 6.1.4 $tips = array(10, 20, 30, 30); show_array("測試陣列:",$tips); $result = array_pad($tips, -5, 40); // 擴充陣列元素 show_array("向左擴充陣列:",$result); $result = array_pad($tips, 5, 50); show_array("向右擴充陣列:",$result); $result = array_unique($tips); // 刪除重複的元素 show_array("刪除重複元素:",$result); $tips = array(10, 20, 30, 30); $offset = 2; array_splice($tips, $offset); // 刪除陣列元素 show_array("刪除位移". $offset."後的元素:",$tips); $tips = array(10, 20, 30, 30); $offset = -1; array_splice($tips, $offset); // 刪除陣列元素 show_array("刪除倒數位移". $offset."後的元素:",$tips); $tips = array(10, 20, 30, 30); $offset = 2; $len = 1; array_splice($tips, $offset, $len); // 刪除陣列元素 show_array("刪除位移". $offset."後的". $len. "個元素:",$tips); $tips = array(10, 20, 30, 30); array_splice($tips, 1, 1, array(40, 50)); // 取代陣列元素 show_array("刪除1個元素後取代陣列:",$tips); $tips = array(10, 20, 30, 30); $result = array_reverse($tips); // 反轉陣列 show_array("反轉陣列:",$result); list($var1, $var2, $var3) = $tips; // 將陣列轉換成變數 echo "0: $var1, 1: $var2, 2: $var3<br>"

  10. outline • PHP 陣列宣告 • 一維陣列 • 結合陣列 • 陣列收尋與排序 • 字串函數

  11. 結合陣列函數 6.2.3 $arr = array( "color" => "black", "name" => "陳會安", "shape" => "triangle", "fruit" => "apple" ); print_r(array_keys($arr)); // 顯示鍵值 echo "<br>"; print_r(array_values($arr)); // 顯示元素值 echo "<br>"; // 使用內部陣列指標走訪陣列 reset($arr); // 重設陣列元素到開頭 echo "第一個元素: " . current($arr) . "/". key($arr). "<br>"; next($arr); // 下一個元素 echo "下一個元素: " . current($arr) . "/". key($arr). "<br>"; end($arr); // 最後一個元素 echo "最後一個元素: " . current($arr) . "/". key($arr). "<br>"; prev($arr); // 前一個元素 echo "前一個元素: " . current($arr) . "/". key($arr). "<br>"; reset($arr); // 走訪結合陣列 while (list($key, $val) = each($arr)) echo "$key = $val "; echo "<br>";

  12. outline • PHP 陣列宣告 • 一維陣列 • 結合陣列 • 陣列收尋與排序 • 字串函數

  13. $tips = array(300, 240, 50, 100, 56, 400); sort($tips); // 排序索引陣列元素 foreach($tips as $ele) // 顯示陣列元素 echo "$ele "; echo "<br>";// 指定陣列元素 $colors = array("d"=>"blue","a"=>"red", "b"=>"green","c"=>"white"); asort($colors); // 排序值 // 顯示陣列元素 while (list($key, $val) = each($colors)) echo "$key = $val "; echo "<br>"; // 指定陣列元素 $fruits = array("d"=>"lemon","a"=>"orange", "b"=>"apple","c"=>"banana"); ksort($fruits); // 排序鍵值 while (list($key, $val) = each($fruits)) echo "$key = $val "; 陣列排序 6.3.2

  14. $tips = array(300, 240, 50, 100, 56, 400); $names = array("Joe", "Mary", "Jane", "Tom"); $colors = array("d"=>"blue","a"=>"red", "b"=>"green","c"=>"white"); // in_array()函數 if (in_array("50", $tips)) { print "陣列\$tips有50<br>"; } if (in_array("Tom", $names)) { print "陣列\$names有Tom<br>"; }// array_search()函數 if ( ($index = array_search("Jane", $names)) != false) { print "陣列\$names有Jane => $index<br>";} if ( ($k = array_search("red", $colors)) != false) { print "陣列\$colors有red => $k<br>"; } 陣列搜尋 6.3.3

  15. outline • PHP 陣列宣告 • 一維陣列 • 結合陣列 • 陣列收尋與排序 • 字串函數

  16. $str1 = "username@company.com.tw"; $str2 = 'C:\PHP4\Ch06'; print("測試的字串1: \"" . $str1 . "\"<br>"); print("測試的字串2: \"" . $str2 . "\"<br>"); // 取得ASCII碼的字元 $A = chr(65); echo "ASCII碼65是字元: $A <br>"; echo "ASCII碼56是字元: ". chr(56). "<br>"; // 取出子字串 $domain = substr(strstr($str1, '@'), 1); print "網域名稱 => " . $domain . "<br>"; $dir = substr($str2, 2, 6); print "substr(\$str2, 2, 6)取得路徑 => " . $dir . "<br>"; // 計算次數 $no = substr_count($str1, "com"); print "計算字串1中com出現的次數 => " . $no . "<br>"; // 取代字串 $str = substr_replace($str2, "TEST", 3, 4); print "取代字串2的PHP4 => " . $str . "<br>"; // 反轉字串 print "反轉字串1 => " . strrev($str1) . "<br>"; // 重複字串 print "重複字串2一共2次 => " . str_repeat($str2, 2) . "<br>"; // 將字串以分隔字元轉換成陣列 $userlist = "江小魚||陳會安||小龍女||張無忌"; print "原始字串: " . $userlist . "<br>"; $users = explode("||", $userlist); print_r($users); // 將陣列元素輸出成字串 $user_string = implode("||", $users); print "組成的字串: " . $user_string . "<br>"; 子字串和字元的處理 6.4.3

  17. $d = getdate(); $date = $d["year"]."/".$d["mon"]."/".$d["mday"]; echo "日期字串: " . $date . "<br>"; // 檢查日期格式 if (ereg("([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})", $date, $regs)){ echo "日期: ".$regs[3]."-".$regs[2]."-".$regs[1]."<br>"; echo "日期: ".$regs[0]. "<br>"; }else echo "不合法的字串格式<br>"; // 分割字串 $array = split("/", $date, 3); echo "年: ". $array[0]."<br>"; echo "月: ". $array[1]."<br>"; echo "日: ". $array[2]."<br>"; // 字串變數 $str = "This is a book."; print("測試的字串: \"" . $str . "\"<br>"); if (eregi("k", $str)) // 是否擁有子字串 echo "'$str 字串擁有字元 'k' 或 'K'<br>"; // 取代字串 $newstr = ereg_replace("is", ":", $str); echo $newstr . "<br>"; 正規運算的字串函數 6.5.2

More Related