StructKeyArray

Returns an array of the keys in the specified ColdFusion structure.

See also StructClear, StructDelete, StructFind, StructInsert, StructIsEmpty, StructKeyList, StructKeyExists, StructCount, and StructUpdate.

Syntax

StructKeyArray(structure)
structure

Structure from which the list of keys is to be extracted.

Usage

The array of keys returned by StructKeyArray is not in any particular order. In order to sort keys alphabetically or numerically, use ArraySort.

Note that this function throws an exception if structure does not exist.

Example

<!--- 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>

<BODY  bgcolor="#FFFFD5">

<H3>StructKeyArray Example</H3>
<H3>Extracting the Keys from the Employee Structure</H3>      

<!---------------------------------------------------------------- 
This section of code creates the new structure and checks to 
see if the submit button has been pressed.  If it has been
pressed, the code defines fields in the employee structure
with what the user has entered from 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> 
    </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>