Arrays 陣列

Arrays 陣列

What's an Array? 什麼是陣列?


An array is a list of items, a bit like a shopping list. It allows you to store more than one item in only one variable.
PHP是一種網頁程式語言可用來執行以下這些任務:驗證從瀏覽器傳回的資料.生成自訂的網頁內容以提供給前台.和資料庫溝通,甚至是發送或接收cookies(小包裝的數據,用來讓瀏覽器能夠記住一些事情,比如Codecademy就是用cookies來記住你已經完成登入)

Think of it like this. When writing your shopping list, you could use a separate piece of paper for each item you need to buy (a variable). However this is silly and unneeded—could you imagine how hard it would be to carry all that paper around with you? So, you use one piece of paper for all of your items. This one piece of paper is your array.
想像一下。當你在列購物清單的時候,你當然能夠把每一個物品都寫在單獨的一張紙上(就像是變數)。但是這是很愚蠢且沒必要的--你何不想像一下你需要帶一大疊的紙才能列清楚要買哪些東西?你應該把所有的購物內容都寫在一張單子上,而這一張紙就是陣列。

In the editor do you see the bit of text that starts with $array =? That is our array. Don't worry about all the details just yet, we will explain in more detail later. For now, just see if you can work out what is happening.
在編輯器你有沒看到有行指令是以 $array = 開頭?那就是陣列。先不用擔心裏頭的細節,我們會在後續解說清楚。現在你只要知道這是甚麼即可。

Check out the code in the editor. Looks familiar, doesn't it? That's because a lot of it is regular old HTML! The PHP code is written in the <?php and ?> . See how it generates numbers, creates lists, and adds text directly to your webpage?

看一看在右方編輯器的程式碼,是否有點熟悉?這是因為絕大多數都是尋常的Html碼!而PHP程式碼則是被寫在了 <?php ?> 之間。想看看它是如何生成數字.創建列表,並且在你的網頁上加入文字?


Instructions 操作指引

Once you are ready to move on, click Save & Submit Code.
當你準備好要繼續學下去,按下 Save & Submit Code



Array Syntax 陣列語法


Have you noticed something familiar at the start of our array? That's right, it starts in the same way as a variable, with the $ sign, and then a name, followed by =.
你是否注意到在陣列的開頭有甚麼東西似曾相似? 那就對了,陣列宣告的開頭和變數相同,用一個$開頭,給它一個名稱,在接著寫一個 = 表示賦值(意思就是把等號右邊的內容存到左邊的容器裡頭)。

However, this is when things start to get different. When declaring an array, we have to use array(). This basically tells PHP that $array is an array and not something else, such as a regular old variable.
然而,接下來的內容開始有些不同。當宣告一個陣列,我們必須使用 array()。這基本告訴PHP這是一個指向陣列的變數,其他也就變數一樣,沒啥好說的。

PS:其實實務上也能透過 [ ]語法來完成陣列宣告

By now, I am sure you have noticed the text inside the ( and ). This is just the items in our array. So, currently, our array has the items "Egg," "Tomato," and "Beans" in it. You can add any type of information to an array, and you do it in much the same way as when declaring variables. Use "" when adding strings, and just enter the number when adding integers.
現在,我想你門應該有注意到寫在 ( 和 ) 裡面的字串。這些就是在陣列裏頭的元素。那麼,目前為止我們的陣列裏頭有 "Egg"."Tomato"和"Egg"。你可以加入任何類型的元素到陣列裏頭,這就跟你存入資料到變數一樣,字串資料要加雙引號,數字則不用加。

You must always remember, however, that each item in an array must be separated by a comma: ,.
但是,你永遠要記得,每一個元素之間都必須要用逗號來加以隔開。


Instructions 操作指引


Add two more items to our array, "Chips"and "Sausage".
再加入兩個元素到我們的陣列當中,分別是 "Chips"和 "Sausage"。


Creating Your First Array 建立你的第一個陣列


Nice work! Now let's try creating an array from scratch
幹的好!現在試著自己從頭建立一個陣列。

Instructions 操作指引

Create an array called $friends and put the names of three of your friends in it. Since each friend's name is a string, make sure to write it between quotes.
建立一個名為 $friends 的陣列,然後放入三個你好朋友的名字做為字串元素,記得元素之間要用逗號隔開。

Click "Stuck? Get a hint!" for an example.
如果需要一個範例,可以按下 "Stuck? Get a Hint!"


Access by Offset with [ ] 利用[ ]來取用元素


Each item in an array is numbered starting from 0. For example, when we create an array:


$myArray = array("do", "re", "mi");

The item "do" is in position 0, the item "re"is in position 1, and so on. 素"do"的元素位置,一般我們稱為索引值(index),為0。元素"re"的元素位置為1,依此類推。

Therefore, we can access a particular item of the array using its position, like this: 因此我們可以根據某個元素的位置來取用它,就像這樣:


$myArray = array("do", "re", "mi");

echo $myArray[0]
// outputs "do"

- First we create an array named $myArray
- 一開始我們建立一個名為$myArray的陣列

- Then we use echo to output the first item in $myArray. Since items are numbered starting from 0, "do" is at position 0.
- 接著我們用 echo 來輸出 $myArray 裏頭的第一個元素。因為元素位置(索引值)從0起算,"do"的元素位置為0。

Instructions 操作指引

In line 8, there's an array named $tens. Use echo to output the third item in $tens. Remember that items are numbered starting from 0.
在第八行,有一個名為$tens的陣列。使用 echo 來輸出 $tens 陣列的第三個元素。記得元素位置是從0開始起算的。


Access by Offset with { } 利用{ }來取用元素


PHP is a very flexible language. When accessing arrays by offset, you can actually use two different types of syntax: the []syntax we've covered, or you can use curly braces ({}). You use the curly braces just like you use the square brackets:
PHP是非常彈性的語言。當想透過元素位置來取用陣列裏頭的元素,其實有兩種寫法。[ ] 語法剛才已經說明過了,另一種你可以使用 { },你使用 { } 或使用[ ]語法功能上並無不同。



$myArray = array("do", "re", "mi");
print $myArray{2};
// prints "mi";

Both forms are equivalent, and using one or the other is totally up to you!
這兩種寫法完全相同,要使用哪一種寫法可以自行決定!

(PS:譯者建議選用其中一種就好,不要交互使用,建議使用傳統的[ ] 比較好,和其他程式語言比較相近)

Instructions 操作指引

Go ahead and update your previous code to use the {} syntax instead of [].
繼續下去,將上一個章節的練習做些修改,將 []語法改成{ }語法。


Modifying Array Elements 修改陣列元素


An item in an array can be changed by specifying its position and providing a new value, like this:
陣列中的元素可以被替換,同樣是透過其元素位置來賦予新的內容,像這樣:


$myArray = array("red", "blue", "yellow");

echo $myArray[1];
// outputs "blue"

$myArray[1] = "green";

echo $myArray[1];
// outputs "green"

- First we create a new array $myArray with a list of colors.
- 一開始我們建立一個名為 $myArray 的新陣列,裏頭包含了顏色清單。

- Then we output the item at position 1. Since items are numbered starting from 0, "blue" is at position 1
- 然後我們輸出了元素位置為1的元素內容。因為元素位置從0起算,所以位置為1的應該是 "blue"

- Next we change the item at position 1 to "green".
- 接下來我們改變元素位置為1的元素內容,改成 "green"

- Now when we output the item at position 1, we get "green".
- 現在當我們再一次輸出元素位置為1的元素內容,現在我們得到 "green"。

Instructions 操作指引

In line 8, there's an array named $languages.
在第8行,有一個名為 $languages 的新陣列

- Change an item in the $languages array. You can take your pick!
- 隨便選擇其中的一個元素,改變它的內容!

- Then use echo to output $languages.
- 接著使用 echo 來把變動元素的內容 給印出來


Deleting Array Elements 刪除陣列元素

Finally, you can remove elements using unset:
最後,你可以透過 unset 來移除元素:



$array = array("red", "blue", "green");
unset($array[2]);

You can even delete the whole array:
你甚至可以移除整個陣列:


 unset($array);

Instructions 操作指引

Go ahead and remove "Python" from the $languages array using unset().
繼續下去,將 "Python"從 $languages陣列給移除,利用 unset() 語法

分享這篇文章:

發佈自 林品爵

林品爵
一個堅信程式教育能夠改變下一代未來的狂熱者,青少年揚帆計畫的推動者。十年以上全端程式開發,上百場程式教學經驗,累積學生數達百人以上。 成大學士畢業,使用過J2EE.PHP.MySQL.CoronaSDK.Android.Objective-C.Swift.Python等技術知識,歷任友訊科技總部Java工程師首席.黑快馬股份有限公司研發經理.諾亞數碼娛樂執行長等職位,現任哥布林程式教育學苑創辦人,所開發過專案涵括產品管理系統.POS系統.拍賣平台.擴增實境.問券.社區App等等。 程式啟蒙班講師 . Laravel網頁程式班講師 . App程式班講師

關聯文章:

留下留言