1

I have created an array of data, from which I can loop through specific fields and echo these values out, but what I need to do is add these values to a new array, ultimately allowing me to find the average of the values in the new array. As i've said, I can echo out the data, and I think I've figured how to get the average, if only I can create the new array! Any help would be greatly appreciated as I just can't find the answer anywhere, and I'm running low on talent!

My table contains approx 25 fields, im pulling out a number of rows based on a session variable. In the instance im working on, I need to take just the values from 1 column in the table, and add these to an array. The code below will loop through the values, and echo them out, 1 at a time:-

while ($cdarray=mysql_fetch_array($calldata))   {
echo $cdarray['score_total'];
}

This gives me 25555 which are the 4 values I would expect 25, 5, 5, 5

I've tried

while ($cdarray=mysql_fetch_array($calldata))   {
$cdts = $cdarray['score_total'];
$cdtsar = array($cdts);
}

Which results in $cdts being assigned a value of 5,

Any help greatly appreciated!!

1
  • use array_push to push the elements to the new array Commented Apr 17, 2012 at 10:17

3 Answers 3

1

This will get your data from the array, place it into a new one and calculates the average.

$cdtsar = array();

while ($cdarray=mysql_fetch_array($calldata))   {
 $cdtsar[] = $cdarray['score_total'];
}

$average = array_sum($cdtsar) / count($cdtsar);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much - i've used this, and included the average sum which was how I'd intended to find the average within an 'if' to check the count isn't zero first.
1

It actually prints 25 and 5 and 5 and 5, but there are no spaces in between so it looks like "25555". To verify this yourself:

while ($cdarray=mysql_fetch_array($calldata))   {
    echo $cdarray['score_total'];
    echo " / ";
}

To get the average, you can either use

$sum = $count = 0;
$average = null;
while ($cdarray=mysql_fetch_array($calldata))   {
    $sum += $cdarray['score_total'];
    ++$count;
}

// Make sure to guard against divide by zero
if ($count) {
    $average = $sum / $count;
}

or you might have the database calculate the average for you, if changing the query is an option.

1 Comment

Thank you for this - I've included a version of your zero guard in my code as well.
0

If you want to assign the elements to the new array use like this

$cdtsar = array();
while ($cdarray=mysql_fetch_array($calldata))   {
    array_push($cdtsar,$cdarray['score_total']);
}

To find the average of the array

$sum = array_sum($cdtsar);
$num = sizeof($cdtsar);
$avg = $sum/$num;
echo $avg;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.