Results 1 to 5 of 5

Thread: adding an array together

  1. #1
    scoutt
    Guest

    adding an array together

    I want to add the array that is an input. so it would add like this
    resistor[1] + resistor[2] + ... and so on. But it won't add all of them for some reason.

    if I have 2 inputs of 1000 it only shows 1000 instead of 2000.
    PHP Code:
    $r $resistors[1] + $resistors[2] + $resistors[3] + $resistors[4]
     + 
    $resistors[5] + $resistors[6] + $resistors[7] + $resistors[8]; 
    how do I get it so if there was only 2 input it would add then instead of adding all of them all the time. I tried the for loop but I don't think I did it right.

  2. #2
    scoutt
    Guest
    doh!! I didn't start with 0, so it is adding right (now), I still want to know how to put it in a loop and add them on how many there are in the inputs

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    are we talking PERL here?

    if so...
    PHP Code:
    $total 0;

    foreach 
    $element (@resistor)
    {
        
    total += $element;
    }

    print 
    "$total"
    It's been a while, but I think that works.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    scoutt
    Guest
    nope sorry john my man, php all the way. I should have said something at the beginning, must have been asleep on the toilet on this one.

  5. #5
    scoutt
    Guest
    all fixed thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width