Jump to content

variable/array


karimali831

Recommended Posts

Hi,

 

I need help with this, I am quite new to arrays. This one works fine:

 

echo array_sum(array(34,19,22,17,7,22,8,23,17,31,21,32,7,29,13,7))/count(array(34,19,22,17,7,22,8,23,17,31,21,32,7,29,13,7));

which gives "19..."

 

it gets average number. What I want to put the values in the array in a variable so I do this:

 

$t = "34,19,22,17,7,22,8,23,17,31,21,32,7,29,13,7";

 

so why can't I use array like this:

 

echo array_sum(array($t))/count(array($t));

 

I need to use variable in array, please someone help.

Link to comment
Share on other sites

Thanks that worked great but what if $t was this...

(it doesn't give the average value)

 

$po=mysql_fetch_array(mysql_query("SELECT SUM($score1) as wonpoints FROM ".PREFIX."cup_matches WHERE matchno='".$ID."' AND $score1 > $score2 AND ($alpha_groups) AND (clan1='".$gap['clanID']."' || clan2='".$gap['clanID']."')"));

echo $po['wonpoints'];
//outputs 3419221772282317312132729137

echo $t =$po['wonpoints'].",";
//outputs 34,19,22,17,7,22,8,23,17,31,21,32,7,29,13,7,

Link to comment
Share on other sites

You can build an array directly from the query too, if you prefer.

 

$query = "SELECT SUM($score1) as wonpoints FROM ".PREFIX."cup_matches WHERE matchno='".$ID."' AND $score1 > $score2 AND ($alpha_groups) AND (clan1='".$gap['clanID']."' || clan2='".$gap['clanID'] . '"';
$result = mysql_query($query);
while( $row = mysql_fetch_assoc($result) ) {
     $array[] = $row['wonpoints'];
}

Link to comment
Share on other sites

Ok, is this right?

 

$query = mysql_query("SELECT SUM($score1) as wonpoints FROM ".PREFIX."cup_matches WHERE matchno='".$ID."' AND $score1 > $score2 AND ($alpha_groups) AND (clan1='".$gap['clanID']."' || clan2='".$gap['clanID']."')");
  while( $row = mysql_fetch_assoc($query) ) {
     $array[] = $row['wonpoints'];
     
     echo $row['wonpoints'].",";
     echo array_sum(explode(',', $array))/count(explode(',', $array)).",";
   }

 

first echo

34,19,22,17,7,22,8,23,17,31,21,32,7,29,13,7,

 

2nd echo

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

 

 

now all I want is to get the average number of the first echo, which should be 19 dot...

 

Link to comment
Share on other sites

At that point, you have everything you need in the array named $array, so all you should need is:

while( $row = mysql_fetch_assoc($query) ) {
     $array[] = $row['wonpoints'];
     
     //echo $row['wonpoints'].",";
while( $row = mysql_fetch_assoc($query) ) {
     $array[] = $row['wonpoints'];
     
     //echo $row['wonpoints'].",";
     //echo array_sum($array)/count($array);
   }
     echo array_sum($array)/count($array); // OUTSIDE the loop . . .


 

Link to comment
Share on other sites

it's wrong:

 

$query = mysql_query("SELECT SUM($score1) as wonpoints FROM ".PREFIX."cup_matches WHERE matchno='".$ID."' AND $score1 > $score2 AND ($alpha_groups) AND (clan1='".$gap['clanID']."' || clan2='".$gap['clanID']."')");
  while( $row = mysql_fetch_assoc($query) ) {
     $array[] = $row['wonpoints'];
     
     //echo $row['wonpoints'].",";
     
   }
     echo array_sum($array)/count($array)."<br>";  

 

the echo outputs:

 

34

26.5

25

23

19.8

20.1666666667

18.4285714286

19

18.7777777778

20

20.0909090909

21.0833333333

20

20.6428571429

20.1333333333

19.3125

 

looks like the last value is correct, average value of all values in array.

Not sure why it is giving me all these other values??

Link to comment
Share on other sites

so no ideas why it is producing other values ? this is your code saved:

 

$query = mysql_query("SELECT SUM($score1) as wonpoints FROM ".PREFIX."cup_matches WHERE matchno='".$ID."' AND $score1 > $score2 AND ($alpha_groups) AND (clan1='".$gap['clanID']."' || clan2='".$gap['clanID']."')");
  while( $row = mysql_fetch_assoc($query) ) {
     $array[] = $row['wonpoints'];   
   }
     echo array_sum($array)/count($array)."<br>"; 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.