Page 1 of 1

Maximum and minimum value

Posted: Fri Jul 27, 2007 3:59 am
by sandy1028
feyd | 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]


Hi,

I want to find the maximum and minimum, average value from the text file

Code: Select all

<?
$lines = file('./data.txt');

                foreach ($lines as $line) {
                $text_line = explode(":" , $line);


$text_line1 = explode(" " , $text_line[2]);
array_sum($text_line1);
}
?>
This method I am not able to get the array sum.....
How to get the numerical value of numbers from strings

When I explode the line it is in string format.... I should find the array_sum and max() which I values are in numeric not string


feyd | 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]

Posted: Fri Jul 27, 2007 4:11 am
by onion2k

Code: Select all

<?php
$lines = file('./data.txt');

foreach ($lines as $line) {

    $arrLineValues = explode(":" , $line);
    $arrField2Values = explode(" " , $arrLineValues[2]);

    $min = min($arrField2Values);
    $max = max($arrField2Values);
    $sum = array_sum($arrField2Values);
    $avg = $sum/count($arrField2Values);

}
PHP will automatically treat a string as it's numerical equivalent in a function that handles numbers.

Oh, and don't use short tags (eg <? ).

max and min Value

Posted: Fri Jul 27, 2007 5:02 am
by sandy1028
feyd | 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]


The values fetched is for the last row and values are not exact....

The values are average and maximum values I have to pass into array

Code: Select all

<?php
$lines = file('./phonedb.txt');
$count= count($lines);
foreach ($lines as $line) {
   $arrLineValues = explode(":" , $line);
   $arrField2Values = explode(" " , $arrLineValues[2]);

    $max = max($arrField2Values);
    $sum = array_sum($arrField2Values);
    $avg = $sum/$count;

?>
$graphValues=array($avg,$max);





onion2k wrote:

Code: Select all

<?php
$lines = file('./data.txt');

foreach ($lines as $line) {

    $arrLineValues = explode(":" , $line);
    $arrField2Values = explode(" " , $arrLineValues[2]);

    $min = min($arrField2Values);
    $max = max($arrField2Values);
    $sum = array_sum($arrField2Values);
    $avg = $sum/count($arrField2Values);

}
PHP will automatically treat a string as it's numerical equivalent in a function that handles numbers.

Oh, and don't use short tags (eg <? ).

feyd | 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]

Posted: Fri Jul 27, 2007 5:47 am
by onion2k
" $avg = $sum/$count;" is not what I posted for the average.

Posted: Fri Jul 27, 2007 5:59 am
by sandy1028
Hi,

$count= number of lines in the array....
The value is correct.....

But when the print the value of average the exact value is not fetched.... For each data the average is calculated and last value is getting fetched








onion2k wrote:" $avg = $sum/$count;" is not what I posted for the average.

Posted: Fri Jul 27, 2007 6:32 am
by onion2k
sandy1028 wrote:$count= number of lines in the array....
The value is correct.....
If that value is correct then you must be trying to get the average value from all the lines in the file. That's not what the code in your first post tries to do. You code splits a line on ":", then takes one of the fields and splits it on " ", then adds all the values from that together. For example:

Code: Select all

Hello:Chris:1 2 3 4:another field:Cat
You're adding up 1 2 3 4. I assumed that was what you want to do. Hence my code returns the min, max and average values from that array, it does not use the number of lines in the file.

I think you need to explain exactly what you're trying to do.