Results 1 to 6 of 6

Thread: Arrays

  1. #1
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arrays

    Hey DD,

    I got a question I've been curious to know the answer to for several months, but have never looked into finding that answer.

    With arrays, is there a way to add a variable to an already existing array?
    For example:
    Code:
    var fruits=new Array();
    fruits[0]="apples"
    fruits[1]="bananas"
    fruits[2]="oranges"
    Is it possible to then go and using a function add a fruits[3]="pears" to that already existing array?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You can do that anytime after the array is created by simply doing this:

    fruits[3]="pears"
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Or, fruits.push("pears")
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Wow....really?
    For some reason I have a vague memory of seeing some javascript function that added to an array. wow....I'm shocked I feel stupid hahahah.
    Is that all that's needed though? Do you have to have it like this?
    Code:
    var fruits=new Array();
    fruits[0]="apples"
    fruits[1]="oranges"
    fruits[2]="bananas"
    fruits[3]=""
    fruits[4]=""
    In order to add to the array? or does it only have to go as high up as i have variables for, then i can add to it with fruits[3]="pears" whenever?

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You can do this:

    Code:
    var fruits=new Array();
    fruits[3]="pears"

    Then later do this:


    Code:
    fruits[0]="apples"
    fruits[1]="oranges"
    fruits[2]="bananas"
    Arrays are very flexible.

    If after you've done the above, you want to insert a new number two and have all the other ones (except 0 and 1) get bumped up a number, that's when things get a little tricky.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Allright, great.

    Thanks.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •