Jump to content

Get avarage of array i=>x values, as a new index value.. please help


iskall

Recommended Posts

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 :P...

 

Thanks for your inputs!

 

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);


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.