Returns an array of the keys in a ColdFusion structure.
StructKeyArray(structure)
StructClear, StructDelete, StructFind, StructInsert, StructIsEmpty, StructKeyExists, StructKeyList, StructCount, StructNew, StructUpdate, StructAppend, StructGet, StructSort, StructFindKey, StructClear
| Parameter |
Description |
|---|---|
| structure |
Structure from which to extract a list of keys |
The array of keys returned by StructKeyArray is not in any particular order. To sort keys alphabetically or numerically, use ArraySort.
This function throws an exception if structure does not exist.
<!--- This example shows how to use the StructKeyArray
function to copy the keys from a specified structure to an array.
It also uses the StructNew function to create the structure
and fills its fields with the information the user types
into the corresponding form fields. --->
<html>
<head>
<title>StructKeyArray Function</title>
</head>
<basefont face = "Arial, Helvetica" size = 2>
<body bgcolor = "#FFFFD5">
<H3>StructKeyArray Example</H3>
<H3>Extracting the Keys from the Employee Structure</H3>
<!----------------------------------------------------------------
This section creates the structure and checks whether the submit
button has been pressed. If so, the code defines fields in the
employee structure with what the user entered on the form.
------------------------------------------------------------------->
<cfset employee = StructNew()>
<cfif Isdefined("Form.Submit")>
<cfif Form.Submit is "OK">
<cfset employee.firstname = FORM.firstname>
<cfset employee.lastname = FORM.lastname>
<cfset employee.email = FORM.email>
<cfset employee.phone = FORM.phone>
<cfset employee.company = FORM.company>
<cfelseIf Form.Submit is "Clear">
<cfset rc = StructClear(employee)>
</cfif>
</cfif>
<P>
This example uses the StructNew function to create a structure
that supplies employee information. The data structure is called
"employee" and its fields are filled with the contents of the
following form. After you have entered employee information into the
structure, the example uses the <b>StructKeyArray</b> function to copy
all of the keys from the structure into an array.
</P>
<hr size = "2" color = "#0000A0">
<form action = "structkeyarray.cfm" method = "post">
<table cellspacing = "2" cellpadding = "2" border = "0">
<tr>
<td>First Name:</td>
<td><input name = "firstname" type = "text"
value = "" hspace = "30" maxlength = "30"></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input name = "lastname" type = "text"
value = "" hspace = "30" maxlength = "30"></td>
</tr>
<tr>
<td>EMail</td>
<td><input name = "email" type = "text"
value = "" hspace = "30" maxlength = "30"></td>
</tr>
<tr>
<td>Phone:</td>
<td><input name = "phone" type = "text"
value = "" hspace = "20" maxlength = "20"></td>
</tr>
<tr>
<td>Company:</td>
<td><input name = "company" type = "text"
value = "" hspace = "30" maxlength = "30"></td>
</tr>
<tr>
<td><input type = "submit" name = "submit"
value = "OK"></td>
<td><b>After you submit the FORM, scroll down to see the array.</b>
</td>
</tr>
</table>
</FORM>
<cfif NOT StructISEmpty(employee)>
<hr size = "2" color = "#0000A0">
<cfset keysToStruct = StructKeyArray(employee)>
<CFLOOP index = "i" from = "1" to = "#ArrayLen(keysToStruct)#">
<P><cfoutput>Key#i# is #keysToStruct[i]#</cfoutput></P>
<P><cfoutput>Value#i# is #employee[keysToStruct[i]]#</cfoutput>
</P>
</CFLOOP>
</cfif>
</body>
</html>