Another helper function for ColdFusion, this one to replicate PHP's array_merge which allows you to merge multiple arrays. In this case I'm only concerned with associative arrays, which ColdFusion calls structs.
CFMX provides StructAppend which will merge two (and only two) structures, optionally overwriting keys. I always want to overwrite keys, and I want to merge unlimited structures.
<cfscript>
function struct_merge() {
var base = {};
var i = 1;
for( i = 1; i LTE ArrayLen(arguments); i=i+1 ) {
if( IsStruct(arguments[i]) ) {
StructAppend(base, arguments[i], true);
}
}
return base;
}
</cfscript>
Usage
<cfscript>
one = {
a = "a",
b = "b",
c = "c",
d = "d",
e = "e"
};
two = {
1 = "one",
2 = "two",
3 = "three",
4 = "four",
a = "one_SetByTwo",
c = "three_SetByTwo"
};
three = {
a = "one_SetByThree",
2 = "b_SetByThree"
};
new = struct_merge(one, two, three);
</cfscript>
<cfdump var=#new#>
Result:

Tags: Code, ColdFusion, Development, PHP

May 9th, 2008 at 3:38 am
Hey how about sharing this and related stuff you've been playing with at CHUG next Thursday: http://capitolhillusergroup.org/ ? Remember, we're meeting virtually now at http://adobechats.adobe.acrobat.com/chug...