進階 WWW 程式設計 -- PHP Array 靜宜大學資訊管理學系 蔡奇偉副教授

Slides:



Advertisements
Similar presentations
新目标初中英语 七年级下册. Unit 8 I’d like some noodles. Section B Period Two.
Advertisements

第 4 章 PHP 基本語法.
十五條佛規 後學:張慈幸
Memory Pool ACM Yanqing Peng.
完形填空技巧 CET4.
Game Check Your Vocabulary.
饮食治疗篇.
输出数组 Result: Array (     [0] => zzu     [1] => php     [2] => MySQL     [3] => javascript     [4] => html     [5] => css )
Unit 8 How do you make a banana milk shake?
The keys to Unit 2 Section A 趣味英语
Operators and Expressions
Ch07 PHP程式基礎 網頁程式設計.
Chapter 7 Search.
Tree(樹) 什麼是「樹」? 「樹」的範例 「樹」的定義 「樹」的表示法.
關聯式資料庫.
Chapter 4 歸納(Induction)與遞迴(Recursion)
第二章 C# 基础知识.
樹狀結構 陳怡芬 2018/11/16 北一女中資訊專題研究.
Dr. Baokun Li 经济实验教学中心 商务数据挖掘中心
第5章 PHP数组.
第五讲 数据的分组、合并与转换.
Chap 3 堆疊與佇列 Stack and Queue.
第8章 列舉器與集合 注意: 本投影片僅供本書上課教師使用,非經同意請勿上網轉載或供拷貝.
C 程式設計— 控制敘述 台大資訊工程學系 資訊系統訓練班.
Matlab M檔案 方煒 台大生機系.
C 語言簡介 - 2.
Dì二十課 看bìng Dì二十课 看bìng
PHP 程式流程控制結構.
I believe everyone will be the best,so let us study hard together!
进程操作.
客户服务 询盘惯例.
樹 2 Michael Tsai 2013/3/26.
第十五课:在医院看病.
編譯程式設計 期末專題說明 V1.1 May 2004.
句子成分的省略(1).
感謝同學們在加分題建議. 我會好好研讀+反省~
進階 WWW 程式設計 -- PHP 語言結構 靜宜大學資訊管理學系 蔡奇偉副教授 2003
Chapter 5 Recursion.
第三章 C# 基础知识.
重阳节 The Double Ninth Festival
Mechanics Exercise Class Ⅰ
PHP编程基础与实例教程 PHP Fundamentals & Practices
Guide to a successful PowerPoint design – simple is best
在Microsoft Access 下 建立資料庫
BORROWING SUBTRACTION WITHIN 20
3.5 Region Filling Region Filling is a process of “coloring in” a definite image area or region. 2019/4/19.
Speaker: Liu Yu-Jiun Date: 2009/4/29
Chapter 2 基本語法.
VRP工具or-tools调研 王羚宇
第3章 PHP表达式.
「與校長有約」 with普二速
A parable for our times 當代寓言
A parable for our times 當代寓言
程式的時間與空間 Time and Space in Programming
取材 Tommy’s Window slideshow
计算机问题求解 – 论题1-5 - 数据与数据结构 2018年10月16日.
本节内容 Lua基本语法.
磁共振原理的临床应用.
算 (原文:logizomai) 我告诉你们,经上写着说:『他被列在罪犯之中。』这话必应验在我身上;因为那关系我的事必然成就。(路22:37) It is written: 'And he was numbered with the transgressors'; and I tell you that.
第 8 章 标准模板库STL 陈哲 副教授 南京航空航天大学 计算机科学与技术学院.
Ch07. 函式.
动词不定式(6).
怎樣把同一評估 給與在不同班級的學生 How to administer the Same assessment to students from Different classes and groups.

Race Conditions and Semaphore
第6章 PHP基本語法介紹.
Unit 8 How do you make a banana milk shake?
字串 第10章 part I 8/30/2019.
A parable for our times 當代寓言
When using opening and closing presentation slides, use the masterbrand logo at the correct size and in the right position. This slide meets both needs.
Presentation transcript:

進階 WWW 程式設計 -- PHP Array 靜宜大學資訊管理學系 蔡奇偉副教授 2003-2005 2019/5/21 進階 WWW 程式設計 PHP Array 靜宜大學資訊管理學系 蔡奇偉副教授 2003-2005 靜宜大學資管系 蔡奇偉編撰 版權所有 2003-2005

內容大綱 陣列的種類 陣列的產生方式 讀取陣列元素值 字串中展開陣列變數值 改變陣列元素值 foreach 迴圈指令 list() 指令 插入、移除、與置換元素

陣列的種類 PHP 的陣列可用來儲存一組相同或不同型態的資料。PHP 提供下列兩種不同存取方式的陣列: 索引陣列(indexed array) 用一個整數索引來存取陣列中的值。 40 25 3.14 ‘Hello’ 1 2 99 indices 對照陣列(associative array) 用一個字串型態的鍵(key)來存取陣列中的對照值。 ‘lastname’ ‘陳’ ‘firstname’ ‘水扁’ ‘sex’ ‘male’ keys values

陣列的產生方式 一、使用指定敘述 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’;

二、使用 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’);

判別變數是否是陣列型態 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; }

讀取陣列元素值 範例 範例 索引陣列可用以下格式藉索引(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 ‘東坡’ 範例 範例

字串中展開陣列變數值 範例 $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'] = 東坡

改變陣列元素值 範例 範例 索引陣列可用以下格式藉索引(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’] 的值變成 '軾' 範例 範例

取得陣列的元素個數 範例 函式 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;

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.

範例 $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.

範例 $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.

範例 foreach(array(1, 2, 3, 4, 5) as $v) { print "$v\n"; } 輸出結果: 1 2 3 4

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";

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" }

移除陣列與陣列元素 範例 函式 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

插入、移除、與置換元素 在陣列尾端加入新元素 在陣列首/尾端補上同值的新元素 array_fill() 函式 array_shift() 函式 array_unshift() 函式

在陣列尾端加入新元素 註 我們可以用下列的方式在陣列尾端加入一個新元素: $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 的操作。

在陣列首/尾端補上同值的新元素 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

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 )

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")

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" )

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.

進階 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. 靜宜大學資管系 蔡奇偉編撰 版權所有 2003-2005