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 
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto 
Site FAQ 
Sitemap 
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQrelated cookies.

Introduction to Linux  A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free. 


01272010, 01:27 AM

#1

Senior Member
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,434
Rep:

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??



01272010, 04:19 AM

#2

Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep:

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?



Thread Tools 
Search this Thread 


Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off



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

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.

Latest Threads
LQ News

