Page 1 of 1

Arrays and dividing

Posted: Fri Feb 22, 2008 6:20 pm
by gammaman
Mod | Please use

Code: Select all

, [code] and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: <!-- s:arrow: --><img src=\"{SMILIES_PATH}/icon_arrow.gif\" alt=\":arrow:\" title=\"Arrow\" /><!-- s:arrow: --> [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
 
 
I have the following code and what I need help with is changing the line marked below were I am dividing by 3, the size of the array , to be able to divide no matter what size the array.  So I guess I am asking how to put that array into a variable to divide it. 
 

Code: Select all

<html>
 
<?php
 
$student = array (        array (92,100,91),
                array(96,82,77),
                array(60,80,70),
                array(90,90,90) );
 
$test_avg = array ();
 
foreach ($student as $name =>$identity){
 
$counter = 0;
foreach ($identity as $score){
            $counter+=$score;
}
/**
 * THIS LINE HERE IS THE ONE I AM TALKING ABOUT
 */
$test_avg [] = $counter / 3;
 
};
 
arsort($test_avg);
echo "<pre>";
echo 'Student            Grade';
echo "<br/>";
foreach ($test_avg as $name => $test_avg){
        printf ("%u %17s %.1f\n",$name, " ", $test_avg);
        echo "<br/>";
};
echo "</pre>"
?>
</html>
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.

Mod | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Re: help with code

Posted: Fri Feb 22, 2008 6:45 pm
by Christopher
It should be:

Code: Select all

$test_avg [$name] = $counter / 3;

Re: Arrays and dividing

Posted: Fri Feb 22, 2008 6:58 pm
by gammaman
Yes but I cannot divide by 3, I need to divide no matter how many students are in the array. There are three now including student zero, but there might not be three later

Re: Arrays and dividing

Posted: Sat Feb 23, 2008 4:07 am
by Kieran Huggins

Code: Select all

$test_avg[$name] = $counter / count($student);