– Common Function Library Project

arrayOfStructsToStructsOfStructs(array, key)

Last updated June 11, 2009


Tayo Akinmade

Version: 0 | Requires: CF6 | Library: DataManipulationLib

This UDF accepts an array of structures and converts it to a structure of structures. It accepts an array of structures and a key as mandatory arguments. The key must exist in all structures in the array

Return Values:
Returns a struct.


<cfset aArray = arrayNew(1)>

<cfset stStruct1 = structNew()>
<cfset = "Micheal">
<cfset stStruct1.age = 20>

<cfset stStruct2 = structNew()>
<cfset = "Smidt">
<cfset stStruct2.age = 25>

<cfset aArray[1] = stStruct1>
<cfset aArray[2] = stStruct2>

<cfset stStructOfStructs = ArrayOfStructsToStructsOfStructs(array=aArray,key="name"))>
<cfdump var="#stStructofStructs#">


Name Description Required
array An array of structs. Yes
key A key value to use for the new structure. Must exist in all structs in the array. Yes

Full UDF Source:

 Converts an array of structures to an structure of structures,
 @param array 	 An array of structs. (Required)
 @param key 	 A key value to use for the new structure. Must exist in all structs in the array. (Required)
 @return Returns a struct. 
 @author Tayo Akinmade ( 
 @version 0, June 11, 2009 
<cffunction name="ArrayOfStructsToStructsOfStructs" access="public" output="false" returntype="struct" hint="Converts an array of structs to an struct of structs">
		<cfargument name="array" type="array" required="true" hint="An array of structures">
		<cfargument name="key" type="string" required="true" hint="Key to use">
			var stStructOfStructs = structNew();
			var i = 0;
			// loop over array
			for(i=1;i lte arrayLen(arguments.array);i=i+1){
				stStructOfStructs[arguments.array[i][arguments.key]] = arguments.array[i];
		    return stStructOfStructs;
blog comments powered by Disqus


Latest Additions

Kevin Cotton added
May 5, 2016

Raymond Camden added
April 25, 2016

Chris Wigginton added
January 18, 2016

Gary Stanton added
November 19, 2015

Sebastiaan Naafs - van Dijk added
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson