官术网_书友最值得收藏!

Understanding arrays

The array is an interesting variable construct. You can think of an array as a collection of commonly named variables with a numeric index. We will take the last code example and transfer it to arrays. Here we will change the backend in major ways, but the frontend will run completely the same from a user's point of view. If you look closer, you will be able to see that we are now passing a variable from page to page rather than the whole questions as before. Let's check out the code:

<!--- Example: 1_18.cfm --->
<!--- Processing --->
<cfparam name="url.question" default="1">
<cfscript>
question = arrayNew(1);
question[1] = "What is the speed limit?";
question[2] = "What is a car?";
question[3] = "How much is gas?";
answer = arrayNew(1);
answer[1] = "55";
answer[2] = "Depends who you ask!";
answer[3] = "more than before";
</cfscript>
<!--- Content --->
<cfoutput>
<strong>#question[url.question]#</strong><br />
Answer:&nbsp; #answer[url.question]#<br /><br />
</cfoutput>
All Questions
<hr />
<cfloop from="1" to="#arrayLen(question)#" index="iQuestion">
<cfoutput>
<strong>Q</strong>: <a href="?question=#iQuestion#"> #question[iQuestion]#</a><br />
</cfoutput>
</cfloop>

Rather than two lists at the top, we now have two array variable constructs. Before you start assigning variables to an array, you need to declare the variable as an array type. Arrays can be multi-dimensional. The truth is this is very rarely done, but you may find some use case now and then. We will focus on a single-dimensional array. If you forget to pass in the number of dimensions when you declare an array, you will get an error when that code runs. So, do remember to declare the number of dimensions.

Note

The maximum number of dimensions in ColdFusion is three. Remember that a large array in multi dimensions can consume huge amounts of computer memory. Think twice before using multi-dimensional arrays for this reason.

You will find a rich collection of array functions built into ColdFusion. You can use arrayDeleteAt() to remove an item in the middle of an array. There is also an arrayInsertAt() which does the opposite. There are three things that you need to keep in mind when using arrays:

  • Don't remove items in the middle of an array list without using the built-in functions. This could create a missing element and create an error when looping through the array collection.
  • Don't count on an item staying in the same indexed position. It may seem odd that we call the position an index and think the item can move. This is different because unlike the index in a book, arrays are dynamic.
  • The number of items in an array can change. It is best to use the arrayLen() function as we did in the example code in order to know the current length of an array.

Now we will perform a minor rewrite and run the same code as a multi-dimensional array. This is also called an array of structures. Each dimension of the array has structure. This allows for some unique layout of data within your application memory. We will add a CFDump at the end of the code, so the structure created can be presented for view:

<!--- Example: 1_19.cfm --->
<!--- Processing --->
<cfparam name="url.faq" default="1">
<cfscript>
faq = arrayNew(1);
faq[1] = structNew();
faq[1].question = "What is the speed limit?";
faq[1].answer = "55";
faq[2] = structNew();
faq[2].question = "What is a car?";
faq[2].answer = "Depends who you ask!";
faq[3] = structNew();
faq[3].question = "How much is gas?";
faq[3].answer = "more than before";
</cfscript>
<!--- Content --->
<cfoutput>
<strong>#faq[url.faq].question#</strong><br />
Answer:&nbsp; #faq[url.faq].answer#<br /><br />
</cfoutput>
All Questions
<hr />
<cfloop from="1" to="#arrayLen(faq)#" index="iFAQ">
<cfoutput>
<strong>Q</strong>: <a href="?faq=#iFAQ#"> #faq[iFAQ].question#</a><br />
</cfoutput>
</cfloop>
<cfdump var="#faq#">

Now we have the same basic information but this time our browser window shows the array of structures feeding the output:

We have only touched the surface of what you can do with structures, arrays, and loops. The masters of ColdFusion are still finding creative uses for them. They are pretty simple to master and very flexible to implement.

主站蜘蛛池模板: 南华县| 曲靖市| 秭归县| 工布江达县| 鹿泉市| 柳州市| 新兴县| 江门市| 乌兰浩特市| 长丰县| 额济纳旗| 天全县| 白沙| 遵化市| 四子王旗| 布拖县| 湘潭市| 富顺县| 苏尼特左旗| 岑溪市| 巨野县| 泰兴市| 灵丘县| 囊谦县| 剑阁县| 青浦区| 尼木县| 疏附县| 嵊州市| 兰考县| 丘北县| 嘉义县| 兴城市| 芮城县| 通江县| 威远县| 延吉市| 湟中县| 牟定县| 驻马店市| 罗定市|