This lesson closes out the array functions with two related groups: aggregation functions that summarize an array of numbers into a single statistic, and conversion functions that move data between arrays, lists, and structs.
Learning Objectives
After completing this lesson, you'll be able to:
- Summarize a numeric array with arraySum, arrayAvg, arrayMax, arrayMin, and arrayMedian.
- Convert between an array and a delimited list with arrayToList and listToArray.
- Convert an array into a struct with arrayToStruct.
- Combine two arrays with arrayMerge.
- Extract a portion of an array with arraySlice or arrayMid.
Numeric Aggregation
These all expect a purely numeric array — running them on an array containing strings or structs throws an error.
arrayToList — Array to Delimited String
The default delimiter is a comma — pass a second argument for anything else.
listToArray — Delimited String to Array
arrayToList and listToArray are inverses of each other — useful whenever you need to switch between the two representations.
arrayToStruct
Converts an array into a struct, using each array index as the struct's key (as a string).
This is a fairly rare conversion in practice — most real-world code goes the other direction, building arrays of structs (covered in the next module) rather than converting an array into one flat struct.
arrayMerge — Combining Two Arrays
By default, all elements from both arrays are kept and re-indexed. Passing true as a third argument changes this to keep only the first array's value at any position both arrays share.
Extracting a Portion: arraySlice and arrayMid
Both extract a sub-array without modifying the original. arraySlice supports negative offsets (counting from the end); arrayMid doesn't.
arraySlice(arr, -3) grabs the last 3 elements — a quick way to get "the last N items" without calculating arrayLen(arr) - N + 1 by hand.
Real-World Example: Sales Report Summary
Common Beginner Mistakes
Running arraySum or arrayAvg on a non-numeric array
Any string element (even a numeric-looking one stored as text, depending on the engine) or struct in the array can cause these functions to error — keep aggregation arrays purely numeric.
Forgetting listToArray needs a matching delimiter
listToArray("a;b;c") without specifying ";" as the delimiter treats the whole string as one element, since the default delimiter is a comma.
Expecting arrayToStruct's keys to be numbers
The resulting struct's keys are strings ("1", "2", ...) even though they came from numeric array indices.
Best Practices
- Use arrayToList / listToArray when you need to interoperate with something list-based, like a form field or URL parameter.
- Use arraySlice over arrayMid when you might need a negative ("from the end") offset.
- Validate that an array is purely numeric before calling the aggregation functions on user-supplied or external data.
Interview Questions
What's the default delimiter for arrayToList?
A comma.
What do the keys of the struct look like after arrayToStruct?
They're the original array indices, converted to strings.
How do you get the last 3 elements of an array without knowing its length?
arraySlice(arr, -3) — a negative offset counts from the end.
What happens if arrayMerge finds two arrays with values at the same position?
By default, both values are kept and the result is re-indexed. With the optional third argument set to true, only the first array's value at that shared position is kept.
Summary
In this lesson, you learned to summarize numeric arrays with arraySum, arrayAvg, arrayMax, arrayMin, and arrayMedian, convert between arrays, lists, and structs, combine arrays with arrayMerge, and extract portions with arraySlice and arrayMid.
This wraps up arrays for this module — next comes Structures, ColdFusion's key-value data type.
What's Next?
The next lesson covers Structures (Struct) — ColdFusion's key-value data type, the natural complement to arrays.