karimali831 Posted November 28, 2010 Share Posted November 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/ Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2010 Share Posted November 28, 2010 Because $t contains a string, you need to explode the string into an array. <?php $t = "34,19,22,17,7,22,8,23,17,31,21,32,7,29,13,7"; echo array_sum(explode(',', $t))/count(explode(',', $t)); ?> [/cpde] Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140702 Share on other sites More sharing options...
karimali831 Posted November 28, 2010 Author Share Posted November 28, 2010 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, Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140704 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2010 Share Posted November 28, 2010 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140708 Share on other sites More sharing options...
karimali831 Posted November 28, 2010 Author Share Posted November 28, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140710 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2010 Share Posted November 28, 2010 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 . . . Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140715 Share on other sites More sharing options...
karimali831 Posted November 28, 2010 Author Share Posted November 28, 2010 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?? Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140719 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2010 Share Posted November 28, 2010 Me either. There's nothing in the while loop that should be echoing anything. The only thing in there should be this: $array[] = $row['wonpoints'];. If there's anything else, remove it (and make sure you upload the right file) . . . Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140721 Share on other sites More sharing options...
karimali831 Posted November 28, 2010 Author Share Posted November 28, 2010 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140724 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2010 Share Posted November 28, 2010 Is that all that's in that script, or is there other code also? Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140726 Share on other sites More sharing options...
karimali831 Posted November 29, 2010 Author Share Posted November 29, 2010 there is more as you can see I use $gap['clanID'] in query. It is a long file. Is this the reason then? Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140727 Share on other sites More sharing options...
karimali831 Posted November 29, 2010 Author Share Posted November 29, 2010 Oh.. I just put echo array_sum($array)/count($array)."<br>"; right at the end of the page and it works perfect. I think it is because the script is in a loop itself? Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140730 Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2010 Share Posted November 29, 2010 Yes. If that is within another loop, it could cause that. Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140731 Share on other sites More sharing options...
karimali831 Posted November 29, 2010 Author Share Posted November 29, 2010 Hmm does that mean I can not use your method? It must be in loop for things to work in my script. Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140733 Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2010 Share Posted November 29, 2010 No, you may have to revise the logic a little. Maybe move one loop outside of the other, or not echo inside the loop, or something like that. Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140734 Share on other sites More sharing options...
karimali831 Posted November 29, 2010 Author Share Posted November 29, 2010 Yh I thought about what I could do, but nothing fits. Nothing more you can do, so thanks very much for your help! I'll try figure something around this... Quote Link to comment https://forums.phpfreaks.com/topic/220083-variablearray/#findComment-1140735 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.