ColdFusion, HowTo Convert a String to an Array?
Given the following stri开发者_如何学运维ng in ColdFusion:
ul[0][id]=main1 &ul[0][children][0][id]=child2 &ul[0][children][0][class]= &ul[1][id]=main3 &ul[2][id]=main4 &ul[3][id]=main5
How can I create an array with the info above?
Thanks
from looking at the string it appears that what you're trying to do is convert a url querystring to an array of structures. this is something that we've done in cfwheels in our dispatcher and it's quite complex. to see how we're doing it, take a look at the dispatcher code:
http://code.google.com/p/cfwheels/source/browse/trunk/wheels/dispatch/request.cfm
the methods to look at are:
$createParams()
$getParameterMap()
$createNestedParamStruct()
$createNewArrayStruct()
It's not entirely clear from that string, but my best guess is that you're looking for something like this. It's an array of structs of structs, following the observation that all your "deeper" arrays (like children
) seem to have only a single element (i.e. there's only a children[0]
, whose value is a struct with keys id
and class
, each of which appears to have only a single value).
ul = [];
ul[1] = {}; // ColdFusion arrays are not zero-indexed
ul[1]['id'] = 'main1';
ul[1]['children'] = {}; // Another struct
ul[1]['children']['id'] = 'child2';
ul[1]['children']['class'] = ''; // blank in your example
ul[2]['id'] = 'main3';
ul[3]['id'] = 'main4';
...etc...
I assume you're somehow parsing that encoded string, and you'd be looping and creating new array elements with each iteration. There are more compact ways to do this.
But this may beg the question: Wouldn't it be better to store your encoded arrays in a standard format, easily encoded and decoded by native CFML functions? Use serializeJSON()
on your array to get the string, and deserializeJSON()
to turn the string back into the array.
Form Utilities cfc sounds like what you need.
http://formutils.riaforge.org/
精彩评论