Forum Moderators: coopster

Message Too Old, No Replies

Adding values to an array

         

smatts9

5:13 pm on Aug 25, 2006 (gmt 0)

10+ Year Member



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.

jatar_k

5:25 pm on Aug 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I am a bit confused, I had a quick fix but then I looked again and have no idea what you are doing

why use this foreach
foreach ($var1 as $var2) {

when var2 isn't even used in the loop?

how are they inseperate arrays? It looks like you would be overwriting the same var on each iteration.

do you just want to add
$statsarray[]=array(1 => $stats);

but then every element would have the same key of 1 and then you should use a counter to increment, or something.

If the only important part is that you get stats into an array, then you could also just do this

$statsarray[] = $stats;

and just initialize statsarray before your foreach like so

$statsarray = array();

though, again, that should be the same value everytime since the rest of the loop does nothing to the value of stats.

eelixduppy

5:29 pm on Aug 25, 2006 (gmt 0)



As for another method, you can also use array_push [us3.php.net].

smatts9

5:47 pm on Aug 25, 2006 (gmt 0)

10+ Year Member



Sorry for the bad explanation but I figured it out with using:
$statsarray[] = $stats;
Thank you.