0

I have a PHP script that returns an array of numbers:

$fh = fopen ("./files/".$file, r);
$filedata= explode('|', fgets($fh));

  echo $filedata[5];

result:

0.23387718200684

3.6163940429688

0.030826568603516

Is there a way to get the script to return results added up as just one number?

3
  • 1
    What gets printed when you do var_dump($filedata[5])? Note that neither fopen nor explode automatically adds numbers. Commented Nov 25, 2011 at 14:41
  • @NullUserException: explode() always returns an array of strings, so it's definitely a string... Commented Nov 25, 2011 at 14:46
  • Could You show us the file with input data? Commented Nov 25, 2011 at 16:31

3 Answers 3

4
echo array_sum($filedata);

I think that should do the trick.

Take a look here for further information. array_sum just sums up all the values in an array.

EDIT The above example would work if your array was a flat array of integers, decimals etc. The following is how you would sum up a 2 dimensional array where the value you want to sum is at index position 5:

var $sum = 0;

foreach($filedata as $dataElement) {
    $sum += $dataElement[5];
}

echo $sum;

The above code assume that $filedata is an array of array's, each array in filedata has a value at index 5 that can be summed up... Is that more along the lines of what you need?

Sign up to request clarification or add additional context in comments.

10 Comments

No, because the number is in $filedata[5]. You'll need a loop to sum up only the 5 indexes of the returned explode arrays.
OK - in which case you'll need a foreach - loop through the array and sum up all values in $filedata[5]. Either that or you could create an array of all the values in $filedata position [5] and then sum those up with the above example.
Ya array_sum dosen't return anything :(
This could be solved in such a nice way in python... sum(x.split('|')[5] for x in f) with f being the file object - too bad PHP lacks the features needed for this :)
:( so I cannot do this in PHP?
|
4

You could use array_sum:

echo array_sum($filedata);

Not working?

As might be your case $filedata[5] actually contains a string with newlines and numbers. In this case you need some extra code. Important is the line endings - these can be different depending on your operating system / file.

$sum = 0;
$sLineEnding = "\r\n";
foreach($filedata as $data)
{
    $parts = explode($sLineEnding, $data);
    $sum += array_sum($parts);
}
echo $sum;

1 Comment

that still returns 4 lines of numbers
0

As I see in your code, you are executing it in command line and you do echo $filedata[5] an you get different lines, this is because you are not paying attention to '\n' character, so what you need is to change that character somehow... there are different ways... In this example I'm changing the "\n" for " " simple space

$fh = fopen ("./files/".$file, r);
$filedata= explode('|', fgets($fh));

// if "\n" is not working try '\n'

// One way
echo str_replace(" ","\n",$filedata[5]);

// Another way
echo implode(" ", explode("\n", $filedata[5]));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.