StructKeyArray

Description

Finds the keys in a ColdFusion structure.

Return value

An array of keys; if structure does not exist, ColdFusion throws an exception.

Category

Structure functions

Syntax

StructKeyArray(structure) 

See also

Structure functions

Parameters

Parameter Description
structure
Structure from which to extract a list of keys

Usage

A structure's keys are unordered.

Example

<!--- Shows StructKeyArray function to copy keys from a structure to an array. 
Uses StructNew to create structure and fills its fields with the
information the user enters in the form fields. --->
<h3>StructKeyArray Example</h3>
<h3>Extracting the Keys from the Employee Structure</h3>   
<!-- Create structure. Check whether Submit was pressed. If so, define fields 
in employee structure with user entries on 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 called
"employee" that supplies employee info. Its fields are filled by 
the form. After you enter employee information in structure, the 
example uses StructKeyArray function to copy all of the keys from 
the structure into an array. </p>
<hr size = "2" color = "#0000A0">
<form action = "structkeyarray.cfm">
<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>

Comments