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


Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.