2

Hey all, title may be abit misleading but i didnt know the correct way to write it.

Basically, how can i do the AS3 equivalent of this php code:

return array('x' => 0, 'y' => 0);
0

3 Answers 3

3

The standard way of doing it is like this. The main thing to remember is that 'Object' in AS3 is almost equivalent to PHP's associative array's.

var obj:Object = {x:0, y:0};

trace(obj['x']); // like in PHP
trace(obj.x); // also valid

// AS3 version of foreach in PHP
for(var key:String in obj) {
   trace(key +" = " + obj[key]);
}
Sign up to request clarification or add additional context in comments.

Comments

2
private var map:Dictionary = new Dictionary();
map["x"] = 0;
map["y"] = 0;

1 Comment

Dictionary has some good advantages over a plain Object, but has 2 main disadvantages: 1. more overhead, and 2. more code to write (can't just do {x:0,y:0}, can only use the syntax as shown above)
0

You can do something like this

var myArray:Array = new Array({x:'0'},{y:'1'},{x:'2'});

or

var myArray:Array = new Array({x:'0',y:'1'},{a:'1',b:'2'});

3 Comments

You can use objects as an associative arrays but why are you putting these objects inside an a new Array?
This is done so that the array is contiguous. This way you are able to maintain the index of your array. If you do not care about the index of the array, then using an object is better.
The weird thing about this method is that you now need to do myArray[0]['x'], myArray[1]['y'], and so on, instead of just myArray['x'] or myArray['y']. Most people don't do it this way and just use a plain Object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.