- Introduction
- Creating an Array
- Adding an Element to an Array
- Displaying a Value in an Array
- Looping over an Array
- Manipulating Array Values
- Sorting an Array
- Multidimensional Arrays
- Creating Arrays with More Than Three Dimensions
- Array Aggregate Functions
- Array Utility Functions
- Array Information Functions
3.10. Array Utility Functions
You want to perform utility functions on arrays.
Technique
Use ColdFusion array utility functions.
<cfset aNewArray = ArrayNew(3)>
Comments
You have used a number of CFMX array utility functions in various techniques throughout this chapter. Table 3.2 defines each of the array utility functions, including ones you haven't yet used. Don't worry if you don't understand them all yetthe more you work with arrays, the more you'll find the need to reference this table.
Table 3.2 ColdFusion Array Utility Functions
Function |
Return Value |
ArrayAppend(array, value) |
Places value in the (array length plus 1) element of the specified array. Returns true if successful. |
ArrayClear(array) |
Deletes all values from the specified array. Returns true if successful. |
ArrayDeleteAt(array, index) |
Deletes the value at index in the specified array. All elements with an index greater than index will be offset by -1. Returns true if successful. Throws InvalidArrayIndexException if index is not a valid index for array. |
ArrayInsertAt(array, index, value) |
Inserts value at index of array. The index for elements with an index greater than index will be offset by 1. Returns true if successful. Throws the InvalidArrayIndexException exception if index is not a valid index for array. |
ArrayNew(dimension) |
Creates an array of the specified dimension, which must be 1, 2, or 3. |
ArrayPrepend(array, value) |
Places value in first element of specified the array. All indexes are offset by 1. Returns true if successful. |
ArrayResize(array, size) |
Resets array length to the specified size. Returns true if successful. |
ArraySet(array, startIndex, endIndex, value) |
Sets elements from index startIndex to endIndex to specified value in array. Returns true if successful. |
ArraySort(array, sortType [,order]) |
Sorts all elements in array by sortType (numeric, text, or textnocase) in specified order (asc or desc). If array contains anything other than simple values, ArraySort throws ArraySortSimpleValueException exception. If array contains non-numeric values and sortType is numeric, ArraySort throws ValueNotNumeric exception. Returns true if successful. |
ArraySwap(array, index1, index2) |
Swaps the values at index1 and index2 of specified array. Throws ArraySwapRangeException exception if either index1 or index2 is an invalid index for array. Returns true if successful. |
ArrayToList(array [, delimiter]) |
Returns a list delimited by delimiter (a comma by default) containing the values in array. Throws ArrayNotOneDimensionException exception if array contains more than one dimension. |
ListToArray(list [, delimiter]) |
Returns an array populated with the contents of list using delimiter to specify indexes. Empty values in list are ignored. |