3 Replies Latest reply on Oct 28, 2015 11:47 AM by EddieLotter

    How to output cfquery to array?


      Hello everyone,

      I'm trying to output my cfquery to array. I need something like this:



      <cfset myarray=arrayNew(2)>

      <cfloop query="qryCustomers">

           <cfset myarray = [{"CustomerID":ID,"Name":FirstLast}]>




      I tried this code but that did not work. Error always indicate that something is wrong in my cfset where I have my ID and Customer name together. I'm not sure either if I can do this in cold fusion. I have to put these to values together because I have to use them later to compare with other values from second query. If anyone can tell me what is wrong in my code I would appreciate that. Thanks in advance.

        • 1. Re: How to output cfquery to array?
          EddieLotter Level 4
          One way to do it:
          <cfloop query="qryCustomers">
               <cfset myarray[#currentRow#][1] = #ID#>
               <cfset myarray[#currentRow#][2] = #FirstLast#>
          • 2. Re: How to output cfquery to array?
            pirlo89 Level 1

            How I can create a structure first and then add it to an array?


            So I tried something like this but still does not work.



            <cfset recs = StructNew()>

               <cfloop query="qryTest">

                    <cfset recs[currentRow] = StructNew()>

                    <cfset recs[currentRow].Customer = ' ' & qryTest.CustomerID>

                     <cfset recs[currentRow].Number = ' ' & qryTest.Number>



              <cfset records = [{CustomerID = "#CustomerID#",Number = "#Number#"}] />

              <cfset fncResults.data = recs>


            • 3. Re: How to output cfquery to array?
              EddieLotter Level 4

              That's a different question, but here is how you do it:


              <cfset myarray = arrayNew(1)>

              <cfloop query="qryTest">

              <cfset recs = structNew()>

              <cfset recs.Customer = CustomerID>

              <cfset recs.Number = Number>

              <cfset arrayAppend(myarray, recs)>


              <cfdump var="#myarray#">

              (Sorry about the poor formatting.)