|
ColdFusion Home
Basic ColdFusionIntroductionColdFusion Installation ColdFusion Editors ColdFusion Syntax ColdFusion Includes ColdFusion Variables ColdFusion Variable Types ColdFusion If Statements ColdFusion Loops ColdFusion Datasource ColdFusion Database Queries ColdFusion Lists ColdFusion Arrays ColdFusion Redirect ColdFusion Debugging ColdFusion Error Handling Advanced ColdFusionColdFusion MailColdFusion Functions ColdFusion Components ColdFusion Web Services ColdFusion Upload File ColdFusion Read File ColdFusion Write File ColdFusion Append File ColdFusion Rename File ColdFusion Copy File ColdFusion Move File ColdFusion Delete File Cffile Parameters Using cffile Parameters ColdFusion FTP (cfftp) cfftp Cached Connections ColdFusion HTTP (cfhttp) ColdFusion Query of Queries ColdFusion Charts ColdFusion Summary ColdFusion AdminCF AdministratorCF Archive and Deploy CF Scheduled Tasks CF Mini Tutorial ColdFusion BooksColdFusion MX BibleMacromedia ColdFusion MX 7 Web Application Construction Kit |
ColdFusion ArraysColdFusion arrays (or any array) are a fundamental part of most programming languages and scripts. An array is simply an ordered stack of data items with the same data type. Using an array, you can store multiple values under a single name. Instead of using a separate variable for each item, you can use one array to hold all of them. For example, say you have three Frequently Asked Questions that you want to store and write to the screen. You could store these in a simple variable like this:
This will work fine. But one problem with this approach is that you have to write out each variable name whenever you need to work with it. Also, you can't do stuff like loop through all your variables. That's where arrays come into play. You could put all your questions into one array. Visualizing ArraysArrays can be visualized as a stack of elements.
Note: Some languages start arrays at zero, others start at 1. ColdFusion arrays start at 1. Creating Arrays in ColdFusionMost languages use similar syntax to create arrays. A ColdFusion array is created by first assigning an array object to a variable name...
So, using our prior example, we could write:
Accessing Arrays in ColdFusionYou can access an array element by referring to the name of the array and the element's index number. Displaying Array ElementsThe following code displays the second element of the array named faq. In this case, the value would be How to create a ColdFusion array?
Modifying the Contents of an ArrayTo add an item to the end of an array, use the ArrayAppend() function:
<cfset ArrayAppend(faq, "How to modify an array?")>
To add an item to the beginning of the array, use the ArrayPrepend() function:
<cfset ArrayPrepend(faq, "How to modify an array?")>
To insert an item in a specific position in the array, use the ArrayInsertAt() function (in this case, the new value is added before position 3):
<cfset ArrayInsertAt(faq, ,3, "How to modify an array?")>
|
Need Website Content?
Get unique, quality digital content for your website.
|
||||||||