chyman-91
chyman-91

Reputation: 263

How to set up a 2D Array

I am having some problems setting up a 2d array in ColdFusion. I set my array using:

<cfset lang=ArrayNew(2)>
<csset lang["ch"]["dealer1"] = "代理 1">
<csset lang["en"]["dealer1"] = "Dealer 1">
.....

But when I dump the array, it is empty. Can anyone help? I don't want to use number to set my language.

Upvotes: 1

Views: 267

Answers (2)

Adrian J. Moreno
Adrian J. Moreno

Reputation: 14859

A ColdFusion struct is a string-based collection of data. There are plenty of functions related to structs that you'll need to learn.

Start off with the root struct that will contain your collection of data:

<cfset lang = structNew() />

Underneath that, you want two more collections of data. Each collection has its own root, which in your case is a language code.

<cfset lang.ch = structNew() />
<cfset lang.en = structNew() />

Now you can being adding keys to each sub-struct:

<cfset lang["ch"]["dealer1"] = "代理 1">
<cfset lang["en"]["dealer1"] = "Dealer 1">

Alternately,

<cfset lang.ch.dealer1 = "代理 1">
<cfset lang.en.dealer1 = "Dealer 1">

Now dump out the contents of the struct lang, and set the page encoding to UTF-8, to visualize how ColdFusion stores the data.

<cfprocessingdirective pageEncoding="utf-8" />

<cfset lang = structNew() />
<cfset lang.ch = structNew() />
<cfset lang.en = structNew() />

<cfset lang["ch"]["dealer1"] = "代理 1">
<cfset lang["en"]["dealer1"] = "Dealer 1">

<cfdump var="#lang#" />

A CFDUMP of struct contents.

This should start you on your way. Dig into those functions and the ColdFusion documentation on structs before you go any further.

Upvotes: 3

Adam Cameron
Adam Cameron

Reputation: 29870

Unlike PHP - from which I suspect you are coming - there is the appropriate difference between an array (ordered and indexed numerically) and a struct (indexed by an arbitrary key value).

So you want something like:

myStruct = {akey="some value", subStruct={someKey="another value"}}; //etc

Upvotes: 3

Related Questions