开发者

ActionScript 3: how to create a array or list like: array["name1"] = "hello" (just like you can do in PHP)

in PHP i am used to create a array with using names as keys like

array["something1"] = "output1";
array["something2"] = "output2";
array["something3"] = "output3";

and then use foreach to let them print or do other things with it like

foreach ($array as $ke开发者_如何转开发y => $value) {
echo "$key = $value";
}

is there something similar possible in AS3?

EDIT:: what also is handy of these vars is that you can do something like this:

GetSomethingString:String = GetTheString(); // lets yust say this returns something2
trace(array[GetSomethingString]); // then this will return output2


What you want is an object and a for ... in statement:

var obj = {'something1': 'output1',
           'something2': 'output2',
           'something3': 'output3'};
for (var key:String in obj){
    trace(key + '=' + obj[key]);
}

EDIT: yes, this also allows:

trace(obj[getTheString()]);


Using an object will in fact work, but I think what you're actually looking for is the Dictionary class; see this link

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜