Arrays and dividing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Arrays and dividing

Post 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]
Last edited by RobertGonzalez on Fri Feb 22, 2008 6:46 pm, edited 1 time in total.
Reason: Changed the title to something more meaningful, added code tags, left notes in the post about these.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: help with code

Post by Christopher »

It should be:

Code: Select all

$test_avg [$name] = $counter / 3;
(#10850)
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: Arrays and dividing

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Arrays and dividing

Post by Kieran Huggins »

Code: Select all

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