LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-27-2010, 01:27 AM   #1
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,434
Blog Entries: 6

Rep: Reputation: 51
Calculating Average in PHP Array?


Guys,

I need help with array in php. All I was having the following columns:

Code:
Time  	4 6 7 10 11 avg 4 6 7 10 11 avg
Now Like the Above there were more variables defined through script:
Code:
$cols = array(
        array(0,1),
        array(1,1),
        array(2,1),
        array(3,1),
        array(4,1),
        array(5,1),
        array(6,1),
        array(0,16),
        array(1,16),
        array(2,16),
        array(3,16),
        array(4,16),
        array(5,16),
        array(6,16),
        array(0,19),
        array(1,19),
        array(2,19),
        array(3,19),
        array(4,19),
        array(5,19),
        array(6,19),
        array(7,2),
        array(8,2),
        array(9,2),
        array(10,2),
        array(11,2),
        array(7,23),
        array(8,23),
        array(9,23),
        array(10,23),
        array(11,23),
        array(7,24),
        array(8,24),
        array(9,24),
        array(10,24),
        array(11,24),
        array(7,25),
        array(9,25),
  array(11,2),
        array(7,23),
        array(8,23),
        array(9,23),
        array(10,23),
        array(11,23),
        array(7,24),
        array(8,24),
        array(9,24),
        array(10,24),
        array(11,24),
        array(7,25),
        array(9,25),
        array(10,25),
        array(11,25),
    );

    echo '<tr><td>'.date('d/m/y H:i:').(date('s')<30?'00':'30').'</td>';
    $last_col = 0;
    $sum = 0;
    $count = 0;
    foreach ( $cols as $col ) {
        $count++;
        if ($col[1]<>$last_col) {
            if ($last_col>0) {
                printf('<td>%0.2f</td>',$sum/$count);
            }
            $last_col=$col[1];
            $count = 0;
            $sum = 0;
        }
        $sum += $data[$col[0]+1][$col[1]];
        echo '<td>'.$data[$col[0]+1][$col[1]].'</td>';
    }
    printf('<td>%0.2f</td>',$sum/$count);
    echo "</tr>\n";
    exit;
}
Now what the above code is doing is calculating average of above values from columns{1-?,4-?)..likewise

Say If I remove two Machine and calculate the avg.
How Gonna I do that??
 
Old 01-27-2010, 04:19 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I think that you need to explain you problem a little more:

What does the array signify, I can see that it is an array of arrays but that doesn't mean anything to me?
What is you expected output?
What do you mean by remove a machine?

Also looking at the code what is the variable $data?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP: Converting a PHP array into an HTML array koosha Programming 4 08-11-2009 08:47 AM
php moving average pcock Programming 4 06-15-2009 08:51 PM
calculating average of a column of numbers in bash hta1984 Programming 4 12-13-2006 11:58 AM
Can't use my PHP array. rblampain Programming 4 06-23-2006 07:19 AM
php array gui10 Programming 2 03-26-2002 02:07 AM


All times are GMT -5. The time now is 07:47 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration