iskall Posted November 21, 2008 Share Posted November 21, 2008 Hi folks, I have an array that contains different values under different keys, each key has different sizes... anyways, I would like to get the avarage value as a new index(value) under the key it belongs to. Here's my code: session_start(); #$vocid = '7517'; $vocid = $_SESSION['username']; $array = array(); $sql = mysql_query("SELECT `Fweek` FROM vocdata WHERE Vocid='$vocid' GROUP BY Fweek"); while($weeks = mysql_fetch_array($sql)) { $cweek = $weeks['Fweek']; $sat_sql = mysql_query("SELECT Satindex FROM vocdata WHERE Fweek = '$cweek' and Vocid='$vocid'"); $sql_whole = mysql_num_rows($sat_sql); while($sat_result = mysql_fetch_array($sat_sql)) { $total = $sql_whole; $array[$cweek][] = $sat_result['Satindex']; } # echo count($array[$cweek]); // Test array count... } dump($array); And here's the dump: $array => Array (9)(| ['25'] => Array (1)| (| | ['0'] = String(1) "9"| )| ['26'] => Array (2)| (| | ['0'] = String(1) "8"| | ['1'] = String(1) "9"| )| ['27'] => Array (4)| (| | ['0'] = String(1) "4"| | ['1'] = String(1) "7"| | ['2'] = String(1) "5"| | ['3'] = String(1) "7"| )| ['28'] => Array (4)| (| | ['0'] = String(1) "6"| | ['1'] = String(1) "4"| | ['2'] = String(1) "4"| | ['3'] = String(1) "8"| )| ['30'] => Array (3)| (| | ['0'] = String(1) "7"| | ['1'] = String(1) "7"| | ['2'] = String(1) "7"| )| ['31'] => Array (3)| (| | ['0'] = String(1) "9"| | ['1'] = String(1) "8"| | ['2'] = String(1) "7"| )| ['32'] => Array (3)| (| | ['0'] = String(1) "8"| | ['1'] = String(1) "8"| | ['2'] = String(1) "7"| )| ['35'] => Array (2)| (| | ['0'] = String(1) "3"| | ['1'] = String(1) "7"| )| ['36'] => Array (1)| (| | ['0'] = String(1) "7"| )) I would like to get it like so: example ['28'] => ['avg'] = string(1) "5,5" Notice, the key 28 has the values: 6,4,4,8 which gives me an avarage (6+4+4+8/4) of 5,5 I wouldnt need to keep the other values so it would be fine to put it in a completley new array (but yet keep the old one). Now this shouldn't be a hard task I presume, but I cant do it ... Thanks for your inputs! Link to comment https://forums.phpfreaks.com/topic/133644-get-avarage-of-array-ix-values-as-a-new-index-value-please-help/ Share on other sites More sharing options...
samshel Posted November 21, 2008 Share Posted November 21, 2008 try this session_start(); #$vocid = '7517'; $vocid = $_SESSION['username']; $array = array(); $sql = mysql_query("SELECT `Fweek` FROM vocdata WHERE Vocid='$vocid' GROUP BY Fweek"); while($weeks = mysql_fetch_array($sql)) { $cweek = $weeks['Fweek']; $sat_sql = mysql_query("SELECT Satindex FROM vocdata WHERE Fweek = '$cweek' and Vocid='$vocid'"); $sql_whole = mysql_num_rows($sat_sql); while($sat_result = mysql_fetch_array($sat_sql)) { $total = $sql_whole; $array[$cweek][] = $sat_result['Satindex']; } $array[$cweek]['avg'] = array_sum($array[$cweek]) / count($array[$cweek]); # echo count($array[$cweek]); // Test array count... } dump($array); Link to comment https://forums.phpfreaks.com/topic/133644-get-avarage-of-array-ix-values-as-a-new-index-value-please-help/#findComment-695288 Share on other sites More sharing options...
iskall Posted November 21, 2008 Author Share Posted November 21, 2008 Sweet, thank you! works like a charm.. Added round(array_sum....) and it prints it as floats, thanks! Link to comment https://forums.phpfreaks.com/topic/133644-get-avarage-of-array-ix-values-as-a-new-index-value-please-help/#findComment-695296 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.