• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Stupid question about arrays

Community Beginner ,
Mar 13, 2012 Mar 13, 2012

Copy link to clipboard

Copied

So I was messing in PHP the last few weeks and I like how they do their arrays, essentially mapping a value to value:

    public function setParams(array $params)

    {

        foreach ($params as $key=>$value) {

            $this->setParam($key, $value);

        }

        return $this;

    }

My question is, if I wanted to do this in AS3.0 I would do something like:

public function setParams(params:Array){

     foreach (params as //something)

     {

          setParams(key:String, value:String);

     }

}

Can some one fill in the //something spot for me, thats where Im stuck >_>

thanks.

TOPICS
ActionScript

Views

897

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 14, 2012 Mar 14, 2012

if the values are arrays, you would use the following to trace all the array elements of all the value arrays:

public function(keyvalueA:Array):Object{

var obj:Object={};

for(var i:int=0;i<keyvalueA.length;i+=2){

obj[keyvalueA]=keyvalueA[i+1];

if(keyvalueA[i+1] is Array){

for(var j:int=0;j<keyvalueA[i+1].length;j++){

trace("value:",i,"element:",j,keyvalueA[i+1]);

}

}

}

return obj;

}

Votes

Translate

Translate
Community Expert ,
Mar 13, 2012 Mar 13, 2012

Copy link to clipboard

Copied

use an associative array (ie, object):

public function(keyA:Array,valueA:Array):Object{

// error check to ensure keyA.length==valueA.length

var obj:Object={};

for(var i:int=0;i<keyA.length;i++){

obj[keyA]=valueA;

}

return obj;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 13, 2012 Mar 13, 2012

Copy link to clipboard

Copied

Not sure if thats what I want.....I mean it looks like im storing key and value in an object when I need to be setting key and value to a already defined set method as showen in my op.

your essentially passing in two arrays, checking the length and storing them in an object...if im correct. where as the array i would pass in would have:

array[key, value, key, value...]

and for every key I need its coresponding value to set it into my method that takes both a key and a value....

Maybe I am missing something?

I sort of get it now as I looked up:

http://livedocs.adobe.com/flex/3/html/help.html?content=10_Lists_of_data_4.html

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 13, 2012 Mar 13, 2012

Copy link to clipboard

Copied

if you want to pass an array of key,value,key,value, then do so.  there's no significant difference from what i showed:

public function(keyvalueA:Array):Object{

var obj:Object={};

for(var i:int=0;i<keyvalueA.length;i+=2){

obj[keyvalueA]=keyvalueA[i+1];

}

return obj;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 14, 2012 Mar 14, 2012

Copy link to clipboard

Copied

these are really helpful answers. How ever my next question would be this. what if value in: key, value, key, value.... is an array?

so:

you code would look something like:

public function(keyvalueA:Array):Object{

     var obj:Object={};

    for(var i:int=0;i<keyvalueA.length;i+=2){

             obj[keyvalueA]=keyvalueA[i+1];

            if(obj[keyvalueA[i+1] is Array){

                      for (var i:int = 0; keyvalueA[i+1].length; i++) //note the keyvalueA[i+1].length obv doesnt work...

                     {

                              //do stuff here

                     }

    }

    return obj;

}

Based on this am I any where near the right path or?....

Thanks for you help

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2012 Mar 14, 2012

Copy link to clipboard

Copied

i showed the code in message 3 that should be used if you had an array of key,value,key,value.  it doesn't matter what value is.  value could be an array, display object reference, another object etc.  that code i showed is still valid.

the only difference in those scenarios is what you do with obj.keyvalue1 (which will be an array, display object reference, another object etc), resp.  if obj.keyvalue1 is an array, you access its elements using array notation:

obj.keyvalue1[0], for example.  of course, obj.keyvalue1[0] could be another array, display object reference, another object etc.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 14, 2012 Mar 14, 2012

Copy link to clipboard

Copied

but what I am asking is that if, in the case of my example: key, value, key , value - if value was an array, to walk through that array is my above code close to correct based off your example and my understanding of asssociative arrays?

I know the type checking is correct but how would I walk through that array if it was an array?

if that makes sense.

So as you said it doesnt matter if value is an array. but for the sake of the example in message 4 lets assume it is. based off your code and my addition to the example (asssuming your code even worked, havent tried it) is that correct?

I would check the type of value if it is an array and then I would walk through it.

The question I am asking aside from "is this correct?" is "how do I walk through value - assuming it was an array"

thanks for your help

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2012 Mar 14, 2012

Copy link to clipboard

Copied

if the values are arrays, you would use the following to trace all the array elements of all the value arrays:

public function(keyvalueA:Array):Object{

var obj:Object={};

for(var i:int=0;i<keyvalueA.length;i+=2){

obj[keyvalueA]=keyvalueA[i+1];

if(keyvalueA[i+1] is Array){

for(var j:int=0;j<keyvalueA[i+1].length;j++){

trace("value:",i,"element:",j,keyvalueA[i+1]);

}

}

}

return obj;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 14, 2012 Mar 14, 2012

Copy link to clipboard

Copied

why thank you. that cleared up my confusion

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2012 Mar 14, 2012

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines