Help adding values to an array

Discussion in 'PHP' started by smatts9, Aug 25, 2006.

  1. #1
    I have a script that uses foreach() like this:

    foreach ($var1 as $var2) {
    $statsarray=array(1 => $stats);
    print_r(array_values($statsarray));
    }

    It goes through the foreach right and the print_r displays all the different stats, but there are in separate arrays, and I need them to be added together so they are all in the same array? Thank you.
     
    smatts9, Aug 25, 2006 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Replace:
    $statsarray=array(1 => $stats);

    with:
    $statsarray[]=array(1 => $stats);

    the empty square brackets mean it's an array and to just append the right hand side of the = on the array. This means that as your code stands, $statsarray will become an array of arrays.

    A bigger problem is that you're using $stats, but it's not define anywhere!
     
    TwistMyArm, Aug 25, 2006 IP