0

in php I could do:

$prices['ford']['mondeo']['2005'] = 4500;
$prices['ford']['mondeo']['2006'] = 5500;
$prices['ford']['mondeo']['2007'] = 7000;

and could do:

echo sizeof($prices['ford']); // output 1
echo sizeof($prices['ford']['mondeo']) //output 3

how can I achieve it in Flash. Flash doesn't seems to like STRINGS as ARRAY KEYS, does IT ?

3 Answers 3

2

To have associative array -like functionality, you can use an Object;

var prices:Object = new Object();
prices.ford = new Object();
prices.ford.mondeo = new Object();
prices.ford.mondeo['2005'] = 4500;
prices.ford.mondeo['2006'] = 5500;
prices.ford.mondeo['2007'] = 7000;

or simply

var prices:Object = {
  ford: {
    mondeo: {
      2005: 4500,
      2006: 5500,
      2007: 7000
    }
  }
};

Actionscript doesn't have a built in function resembling sizeof in php, but you can easily write one of your own:

function sizeof(o:Object):Number {
    var n:Number = 0;
    for (var item in o)
        n++;
    return n;
}

And just use it like in php:

trace(sizeof(prices['ford'])); // traces 1
trace(sizeof(prices['ford']['mondeo'])); // traces3
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, you didn't state which version of as you're using in your question...:) I think you can just leave the :* part out in as2.
Edited the function into an AS2 version.
1

Arrays are indexed by integers in Flash, if you want to index on a string use an Object instead.

3 Comments

can u give me example ? are these objects easily accesible ? is there a equivalent of PUSH() & LENGTH() for Objects ??
ok I got it :) THANKS BUT.... how can I do N level Objects to store data events['abc']=Array(Array('yes','no')) what command will add another array to events['abc'] ??
events.abc.push('may be'); But remember that after this line events.abc contains [['yes', 'no'], 'may be'] instead of ['yes', 'no', 'may be'] as you might expect.
1
var array:Array = [];
array['name'] = "the name";
array['something'] = "something else";
trace(array.length);

It traces 0. So yeah, flash doesn't really like strings as array keys though it is allowed. Array is a dynamic class (like Object) where you can add properties to individual objects as you want. array['name'] = "myname" is same as array.name = "myname".

That said, you can assign an array to array['name'] and read its length.

var array:Array = [];
array['name'] = new Array();
array.name.push(1, 2, 3);
trace(array.length);//traces 0
trace(array.name.length);//traces 3

Comments

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.