1 Reply Latest reply on Jun 15, 2006 12:53 AM by BKBK

    2D arry into a structure

    netpagino
      Hey gang .. I need some help here ..
      How can I turn a 2D array into a structure .. I need the output to be :

      Struct
      Sku
      Oty
      shipTo
      price
      reserveNumber

      Struct
      Sku
      Oty
      shipTo
      price
      reserveNumber


      Code:
      <cfscript>
      myItemArray = arrayNew(2);
      myItemArray[1][1] = "10010-023"; //sku
      myItemArray[1][2] = "25"; //qty
      myItemArray[2][1] = "10010-064";
      myItemArray[2][2] = "10";
      myItemArray[3][1] = "10010-072";
      myItemArray[3][2] = "2";
      //
      variables.shipToAccountNumber = "10000";
      variables.isoCode = "us";
      </cfscript>

      <cfoutput>#arraylen(myItemArray)#</cfoutput>
      <cfset myItemStruct = StructNew()>
      <cfoutput>
      <cfloop index="thisSku" from="1" to="#arraylen( myItemArray )#">
      <cfloop index="thisQty" from="1" to="#arraylen( myItemArray[thisSku] )#">
      <cfset myItemStruct.sku[thisSku][thisQty] = myItemArray[thisSku][thisQty]>
      <cfset myItemStruct.quantity[thisSku][thisQty] = myItemArray[thisSku][thisQty]>
      <cfset myItemStruct.shipToNumber = "">
      <cfset myItemStruct.price = "">
      <cfset myItemStruct.reserveNumber = "">
      </cfloop>
      </cfloop>
      </cfoutput>

      <cfdump var="#myItemStruct#">
        • 1. Re: 2D arry into a structure
          BKBK Adobe Community Professional & MVP
          If there's no more to your array than we see here, you should not make matters more complicated. Simply populate the struct with keys that have meaningful item names. For example, use the item names corresponding to the 3 inner array indices. Let's say indices 1, 2, 3 correspond respectively to the items TV, DVDPlayer and Personal Computer. Then you will have

          <cfscript>
          myItemStruct.TV.sku = "10010-023";
          myItemStruct.TV.qty = "25";
          myItemStruct.DVDPlayer.sku = "10010-064";
          myItemStruct.DVDPlayer.qty = "10";
          myItemStruct.PC.sku = "10010-072";
          myItemStruct.PC.qty = "2";
          </cfscript>