CFLib.org – Common Function Library Project

flattenStruct(stObject[, delimiter][, prefix][, stResult][, addPrefix])

Last updated September 02, 2011

author

Tom de Manincor

Version: 2 | Requires: CF6 | Library: DataManipulationLib

Description:
Recursively loops through a structure with nested structures and builds nested keys and values into a single struct. http://tomdeman.com/blog/UDFs

Return Values:
Returns a structure.

Example:

<cfscript>
    stTest = structNew();
    stTest['test_root_val'] = 1;
    stTest['EC'] = structNew();
    stTest['EC'].bCreateBeanFile = true;
    stTest['EC'].bCreateColdSpringFile = true;
    stTest['EC'].sConfigBeanObjPath = 'models.GlobalConfig';
    stTest['EC'].sColdSpringDefFilePath = '/config/GlobalConfigColdspring.xml.cfm';
    stTest['EC'].sECDefinitionFilePath = '/config/environment.xml.cfm';
    stTest['EC'].sub = structNew();
    stTest['EC']['sub'].test_sub_val = 'test';
</cfscript>

<cfdump var="#flattenStruct(stTest)#" label="result">

Parameters:

Name Description Required
stObject Structure to flatten. Yes
delimiter Value to use in new keys. Defaults to a period. No
prefix Value placed in front of flattened keys. Defaults to nothing. No
stResult Structure containing result. No
addPrefix Boolean value that determines if prefix should be used. Defaults to true. No

Full UDF Source:

<!---
 Builds nested structs into a single struct.
 Updated v2 by author Simeon Cheeseman.
 
 @param stObject      Structure to flatten. (Required)
 @param delimiter      Value to use in new keys. Defaults to a period. (Optional)
 @param prefix      Value placed in front of flattened keys. Defaults to nothing. (Optional)
 @param stResult      Structure containing result. (Optional)
 @param addPrefix      Boolean value that determines if prefix should be used. Defaults to true. (Optional)
 @return Returns a structure. 
 @author Tom de Manincor (tomdeman@gmail.com) 
 @version 2, September 2, 2011 
--->
<cffunction name="flattenStruct" access="public" output="false" returntype="struct">
    <cfargument name="original" type="struct" required="true"><!--- struct to flatten --->
    <cfargument name="delimiter" required="false" type="string" default="." />
    <cfargument name="flattened" type="struct" default="#StructNew()#" required="false"><!--- result struct, returned at the end --->
    <cfargument name="prefix_string" type="string" default="" required="false"><!--- used in the processing, stores the preceding struct names in the current branch, ends in a delimeter --->
    
    <!--- get this level's elements --->
    <cfset var names = StructKeyArray(original)>
    <cfset var name = "">
    
    <cfloop array="#names#" index="name">
        <!--- add name --->
        <cfif IsStruct(original[name])>
            <cfset flattened = flattenStruct(original[name], delimiter, flattened, prefix_string & name & delimiter)>
        <cfelse>
            <cfset flattened[prefix_string & name] = original[name]>
        </cfif>
    </cfloop>
    
    <cfreturn flattened>
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson