• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

2D Array question

Engaged ,
Jul 16, 2010 Jul 16, 2010

Copy link to clipboard

Copied

Hi,

I have two arrays, call them arrOne and arrTwo

How can I put arrTwo inside of the first position in arrOne


So say I have values like this in arrOne:

<cfset arrOne = ArrayNew(1)>
<cfset arrOne[1] = "John">
<cfset arrOne[2] = "Paul">

<cfset arrTwo = ArrayNew(1)>
<cfset arrTwo[1] = "Fruit">
<cfset arrTwo[2] = "Meat">


I want to put arrTwo inside of arrOne in the first position.  I tried using ArrayInsertAt, but it overwrote the
first position which is not what I want.  The insert shouldn't overwite John's name. It should appear nested under it

I'm trying to somehow get arrTwo into arrOne[1][1]... something like that...

Any help appreciated...

-ws

TOPICS
Advanced techniques

Views

581

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 16, 2010 Jul 16, 2010

Copy link to clipboard

Copied

Initialize your array as 2D.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 16, 2010 Jul 16, 2010

Copy link to clipboard

Copied

CF doesn't really have the concept of multi-dimensional arrays like other languages might, it just effects the same result by allowing any given array element to be, itself, an array.

So there's no trick to doing what you want: just prepend your second array to the first one.

That said, a 2-D array is seldom the best data structure for most requirements. What is the nature of your data? Are you sure you'd not be better off with an array of structs, or possibly having a struct with an element that is an array?

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 20, 2010 Jul 20, 2010

Copy link to clipboard

Copied

LATEST

I would agree with Adam regarding the structures.  However, if you need to go down the array route, you're looking for syntax like this:

<cfset arrTwo = ArrayNew(1)>
<cfset arrTwo[1] = "Fruit">
<cfset arrTwo[2] = "Meat">

<cfset arrOne = ArrayNew(2)>
<cfset arrOne[1][1] = "John">

<cfset arrOne[1][2] = arrTwo>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation