Arrays, like lists, store multiple values within a single variable. Arrays differ from lists in two important ways:
NOTE ColdFusion stores queries, structures, and arrays as complex data. ColdFusion stores strings and numbers as simple data. When to Use ArraysYou should use arrays when the order of something is crucial. A shopping cart, for example, is the perfect candidate for an array because each item is referenced according to the order in which the buyer chose it. Arrays should also be used where descriptive statistics (average, sum, min/max, count) need to be performed on a series of values. When Not to Use ArraysArrays are not ideal for all situations. Because arrays are complex data, a complete array can't be printed or passed without the use of <cfloop>. For situations in which you need to pass a set of data between HTML form pages or print it on an HTML page, lists are preferred. For situations in which the order of values does not matter and simple calculations such as averaging or summation are not necessary, a structure is a better alternative. Similarly, if array members (elements) must be named explicitly, then structures should be used. Arrays are best suited for grouping related data (for example, names of students, grocery lists, and clothing sizes). CAUTION You may hear the term associative array usedthese are structures, not arrays.
Structures will be covered in detail in Chapter 15, "Structures." <cfloop> was described in Chapter 4, "Looping," and lists were discussed in Chapter 13, "Lists." |