开发者

Array Key in ColdFusion Structure

开发者 https://www.devze.com 2023-01-07 17:59 出处:网络
What is the proper synta开发者_JS百科x for creating a key within a ColdFusion structure which is an array? Preferably in the cfscript tags.

What is the proper synta开发者_JS百科x for creating a key within a ColdFusion structure which is an array? Preferably in the cfscript tags.

To give a clearer idea of what I'm trying to do, here's what I thought it might be:

StructInsert(account[i], "child[numChildren]", z);

where "child" was supposed to be an array and numChildren was a counter in a loop.

Obviously this doesn't work. It just gives me an error saying that the key "child[numChildren]" already exists.


You say the "account" structure called "child" which is an array. This doesn't make any sense. If "child" is an array, it cannot be a structure. If it's a structure, it cannot be an array. An array can contain structs, and structs can contain arrays.

A struct is a map or hash, in other words, it consists of name value pairs. An array is a set or list of values. You can loop over them, or access them via their numeric index.

Let's make account an struct, and child an array.

<cfset Account = structNew() />
<cfset Account.Child = ArrayNew(1) />

Account is a struct, so you can use struct functions on it (structKeyExists, structInsert).
Account.Child is an array, so you can use array functions on it (arrayAppend, etc.). Account.Child, being an array, can contain pretty much any value in an entry, including complex values. So let's make Account.Child an array of structs.

let's say z in your example is a structure that looks something like this:

<cfset z = structNew() />
<cfset z.id = 1 />
<cfset z.name = "James" />

You could add this to Account.Child like so:

<cfset ArrayAppend(account.child,z) />

Or, you could do it directly via the index like so:

<cfset account.child[numChildren] = z />

NOW. Lets say you want to keep Account a struct, but you want to have 1 key for each child in the struct, not use an array. You can do this by using a dynamic key, like this:

<cfset Account["child_#numChildren#"] = z />

FYI, structInsert is generally an unnecessary function.


I am not entirely clear on what you are trying to do - but if you wanted to create a child element that is an array inside a struct, here is what you would do:

var s = StructNew();
StructInsert(a, "ArrayName", ArrayNew(1))
a.ArrayName[10] = "Value for index 10";

StructInsert takes the following arguments: (struct, key, value). See this Adobe docs for more info: http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f02.html


If you want it variable, remove the quotes, otherwise the key is that string.


Though it's hard to determine if what you're trying to say is:

Account[i][ child[numChildren] ] = z

vs

Account[i]['child'][numChildren] = z

Or something else.


Update:

Ok, so based on your comment, you probably want this:

Account.Child[numChildren] = z;

Which will be inside a loop with numChildren as index, somewhere after the following statements:

Account = StructNew();
Account.Child = ArrayNew(1);

(You can also use Account['Child'] instead of Account.Child notation.)

0

精彩评论

暂无评论...
验证码 换一张
取 消