learningtech
learningtech

Reputation: 33715

equivalent of php's array_flip in flash actionscript 3?

Is there an equivalent of php's array_flip() in flash actionscript 3? Here's the definition for array_flip:

array_flip() returns an array in flip order, i.e. keys from trans become values and values from trans become keys.

If not, what is the least verbose and most efficient way to achieve the same results as array_flip() in actionscript 3?

Upvotes: 1

Views: 152

Answers (2)

Marty
Marty

Reputation: 39466

Use this function:

function flip(obj:Object):Object
{
    var base:Object = {};

    for(var i:String in obj)
    {
        base[obj[i]] = i;
    }

    return base;
}

Demo:

var array:Array = [];

array["a"] = "a1";
array["b"] = "b2";
array["c"] = "c3";

var newObj:Object = flip(array);

trace(newObj.b2); // b

Upvotes: 1

Chunky Chunk
Chunky Chunk

Reputation: 17237

you can use the for each...in statement to get the value associated to a key and use the for...in statement to get the key associated to a value.

Upvotes: 0

Related Questions