CFLib.org – Common Function Library Project

structToList(s[, delim])

Last updated July 25, 2006

author

Greg Nettles

Version: 2 | Requires: CF5 | Library: DataManipulationLib

Description:
Converts a struct into a delimed key/value list. Defult delimiter is "&" but any character can be used by passing it as a second argument.

Return Values:
Returns a string.

Example:

<!--- Create Struct --->
<cfset user=structNew()>
<cfset user.firstname="John">
<cfset user.lastname="Smith">
<cfset user.age=35>
<cfdump var="#user#" label="user">

<!--- Call Function --->
<br>
Using defult delimiter:
<br>
<cfset myList = structToList(user)>
<cfdump var="#myList#">
<br><br>
Using custom delimiter:
<br>
<cfset myList2 = structToList(user,"@")>
<cfdump var="#myList2#">

Parameters:

Name Description Required
s Structure. Yes
delim List delimeter. Defaults to a comma. No

Full UDF Source:

/**
 * Converts struct into delimited key/value list.
 * 
 * @param s      Structure. (Required)
 * @param delim      List delimeter. Defaults to a comma. (Optional)
 * @return Returns a string. 
 * @author Greg Nettles (gregnettles@calvarychapel.com) 
 * @version 2, July 25, 2006 
 */
function structToList(s) {
    var delim = "&";
    var i = 0;
    var newArray = structKeyArray(arguments.s);

    if (arrayLen(arguments) gt 1) delim = arguments[2];

    for(i=1;i lte structCount(arguments.s);i=i+1) newArray[i] = newArray[i] & "=" & arguments.s[newArray[i]];

    return arraytoList(newArray,delim);
}

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