Skip navigation
Currently Being Moderated

Stupid question about arrays

Mar 13, 2012 11:21 AM

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.

 
Replies
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 13, 2012 11:24 AM   in reply to Adrien.

    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[i]]=valueA[i];

    }

    return obj;

    }

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 13, 2012 12:00 PM   in reply to Adrien.

    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[i]]=keyvalueA[i+1];

    }

    return obj;

    }

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 14, 2012 8:16 AM   in reply to Adrien.

    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.

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 14, 2012 11:48 AM   in reply to kglad

    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[i]]=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][j]);

    }

    }

    }

    return obj;

    }

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 14, 2012 12:18 PM   in reply to Adrien.

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points