Jump to content

maximum and minimum value


sandy1028

Recommended Posts

Hi,

 

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

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/62001-maximum-and-minimum-value/
Share on other sites

Hi,

 

I am facing problem here not able to fetch the exact values of averag and sum

 

It is fetching the last value

 

I should pass the value of avg and maximum in to array

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

 

 

 

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


try this

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

//added
$allNumbers = array();

foreach ($lines as $line) {
  $arrLineValues = explode(":" , $line);
  $arrField2Values = explode(" " , $arrLineValues[2]);
  
  $allNumbers = array_merge($allNumbers, $arrField2Values);
}

  $max = max($allNumbers);
  $sum = array_sum($allNumbers);
  $avg = $sum/count($allNumbers);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.