Search code examples
arrayscoldfusioncfquerycfloop

populate array using cfloop query


I am trying to build an array using a cfloop of query data. I am not sure how to increment the array count vs just overwriting the first value in the array each time? If this doesn't make sense please let me know.

<CFSET MyArray=ArrayNew(1)>
<CFLOOP QUERY="GetPermission">
   <CFIF #GetPermission.Permission_ID GT 10>
        <CFSET MyArray[increment value][GetPermission.Permission_ID]>
   </CFIF>
</CFLOOP>

So my array should look something like MyArray[11,14,24,25,31]


Solution

  • Unless you actually need the index for something else, just omit it and use ArrayApend which "... Appends an array element to the end of a specified array".

     <cfset ArrayAppend( MyArray, GetPermission.Permission_ID)>
    

    As an aside, when you run into a problem like this, it is helpful to peruse the functions by category section of the documentation. Most functions are very well named. Simply picking the right category, and reviewing the function names, often provides the answer right off the bat. Then it is just a matter of reading the usage docs and testing the code.

    Update: However, as discussed on your other thread, ultimately there are better options than arrays for this specific task (ie update permissions list).