creating a multidimensional array

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default creating a multidimensional array

    How can I combine two arrays in cfscript to form one multidimensional array?

    For example, I have two arrays:

    firstarray[1]=fred;
    firstarray[2]=jim;

    secondarray[1]=joe;
    secondarray[2]=sam;

    and would like to turn them into one array:
    combinedarray[1][1]=fred;
    combinedarray[1][2]=jim;
    combinedarray[2][1]=joe;
    combinedarray[2][2]=sam;

    Any help would be greatly appreciated.

    natebaca Guest

  2. Similar Questions and Discussions

    1. Problem with multidimensional array
      Hi! I'm new on this forum, and in the Macromedia Flash development. I've read some books, like the Flash MX 2004 game development (C. S. Murray,...
    2. Multidimensional array: see if 1st key is available
      Hi, I've got an multidimensional array $ret = $country_code; Now I want to see if $countryCode is even in that array, because if it's not, it...
    3. copying a multidimensional array
      Hi: whatz the best way to copy an multidimensional array onto another. I have never used something like clone, just want to know whatz the easiest...
    4. Split multidimensional array into 4 multidimensional arrays
      Hello everyone, I have a multidimensional array that I need to split into 4 multidimensional arrays. I've tried the examples from the...
    5. Sorting a Multidimensional Array
      I have an array like this: $events = array( array( '2003-07-01', 'Event Title 1', '1' //ID Number (not unique) ), array( '2003-07-02',
  3. #2

    Default Re: creating a multidimensional array

    Hi,

    Try using the ArrayAppend function.

    <cfscript>

    Firstarray = ArrayNew(1);

    Secondarray = ArrayNew(1);

    Combinedarray = ArrayNew(2); // even ArrayNew(1) will work

    arrayset(Firstarray ,1,2,"jim");

    arrayset(Secondarray ,1,2,"fred");

    ArrayAppend(Combinedarray ,Firstarray);

    ArrayAppend(Combinedarray ,Secondarray);

    </cfscript>

    <cfdump var="#Combinedarray#">

    Thanks,

    Vamsee

    "natebaca" <webforumsuser@macromedia.com> wrote in message
    news:cvs2pk$kob$1@forums.macromedia.com...
    > How can I combine two arrays in cfscript to form one multidimensional
    > array?
    >
    > For example, I have two arrays:
    >
    > firstarray[1]=fred;
    > firstarray[2]=jim;
    >
    > secondarray[1]=joe;
    > secondarray[2]=sam;
    >
    > and would like to turn them into one array:
    > combinedarray[1][1]=fred;
    > combinedarray[1][2]=jim;
    > combinedarray[2][1]=joe;
    > combinedarray[2][2]=sam;
    >
    > Any help would be greatly appreciated.
    >

    Vamsee Krishna Guest

  4. #3

    Default RE: creating a multidimensional array

    There are two examples listed below.

    Here is an example that loops through the arrays as it builds the new one:

    <CFSCRIPT>
    arr1 = ARRAYNEW(1);
    arr1[1] = "fred";
    arr1[2] = "jim";

    arr2 = ARRAYNEW(1);
    arr2[1] = "joe";
    arr2[2] = "sam";

    array_name_list = "arr1,arr2";

    arr3 = ARRAYNEW(2);
    FOR(list_idx = 1; list_idx LTE LISTLEN(array_name_list); list_idx = list_idx + 1){
    array_name = LISTGETAT(array_name_list,list_idx);
    FOR(arr_idx = 1; arr_idx LTE ARRAYLEN(EVALUATE(array_name)); arr_idx = arr_idx + 1){
    arr3[list_idx][arr_idx] = EVALUATE(array_name&"["&arr_idx&"]");
    }
    }
    </CFSCRIPT>

    <CFDUMP VAR="#arr3#">


    Here is a simpler example that appends the arrays to the new array:

    <CFSCRIPT>
    arr1 = ARRAYNEW(1);
    arr1[1] = "fred";
    arr1[2] = "jim";

    arr2 = ARRAYNEW(1);
    arr2[1] = "joe";
    arr2[2] = "sam";

    arr3 = ARRAYNEW(2);
    tmp = ARRAYAPPEND(arr3,arr1);
    tmp = ARRAYAPPEND(arr3,arr2);
    </CFSCRIPT>

    <CFDUMP VAR="#arr3#">



    Reference:
    How can I combine two arrays in cfscript to form one multidimensional array?

    For example, I have two arrays:

    firstarray[1]=fred;
    firstarray[2]=jim;

    secondarray[1]=joe;
    secondarray[2]=sam;

    and would like to turn them into one array:
    combinedarray[1][1]=fred;
    combinedarray[1][2]=jim;
    combinedarray[2][1]=joe;
    combinedarray[2][2]=sam;

    Any help would be greatly appreciated.
    Rob Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139