CFLib.org – Common Function Library Project

QueryToStructOfArrays(query)

Last updated February 24, 2002

author

Nathan Dintenfass

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Changes a query into a struct of arrays, where the keys of the struct are the column names in the query. Each struct key holds an array of the values from the query. See also QueryToArrayOfStructs.

Return Values:
Returns a structure.

Example:

<CFSET Query = QueryNew("id,name,age")>
<CFLOOP INDEX="X" FROM=1 TO=3>
<CFSET QueryAddRow(Query,1)>
<CFSET QuerySetCell(Query,"ID",X,X)>
<CFSET QuerySetCell(Query,"Name","Name #X#",X)>
<CFSET QuerySetCell(Query,"Age",X+15,X)>
</CFLOOP>
<CFSET Struct = QueryToStructOfArrays(Query)>
<CFDUMP VAR="#Struct#">

Parameters:

Name Description Required
query The query to be transformed. Yes

Full UDF Source:

/**
 * Changes a query into a struct of arrays.
 * 
 * @param query      The query to be transformed. 
 * @return Returns a structure. 
 * @author Nathan Dintenfass (nathan@changemedia.com) 
 * @version 1, February 23, 2002 
 */
function queryToStructOfArrays(q){
        //a variable to hold the struct
        var st = structNew();
        //two variable for iterating
        var ii = 1;
        var cc = 1;
        //grab the columns into an array for easy looping
        var cols = listToArray(q.columnList);
        //iterate over the columns of the query and create the arrays of values
        for(ii = 1; ii lte arrayLen(cols); ii = ii + 1){
            //make the array with the col name as the key in the root struct
            st[cols[ii]] = arrayNew(1);
            //now loop for the recordcount of the query and insert the values
            for(cc = 1; cc lte q.recordcount; cc = cc + 1)
                arrayAppend(st[cols[ii]],q[cols[ii]][cc]);
        }
        //return the struct
        return st;
    }

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