/    Sign up×
Community /Pin to ProfileBookmark

array_sum giving the wrong value?

Anyone have this problem too? If so what’s the problem with it?

I have an excel spreadsheet which I’ve imported into MySQL to make my life easier in picking and choosing the specific columns and rows that I need.

When I sum it up in Excel it gives me 1 number, but when I use array_sum in PHP, it gives me a completely different number. I’ve tried to do a str_replace in case there were any spaces. I’ve checked to make sure that the # of rows matches as well.

Here’s what I’ve done so far…Maybe someone can help out by seeing the steps that I’ve done.

1.) Imported PayPal CSV sheet all with Varchar 500
2.) Added the following code to get array_sum

[code=php]
…..
$gross = array();
while ($jan_row = mysql_fetch_assoc($jan_result)) {
$gross[] = str_replace (” “, “”,$jan_row[‘Gross’]);
}
echo “Gross: “.array_sum($gross).”<br>”;
?>
[/code]

3.) Tried it without any str_replace
4.) did a <pre><?php print_r($gross);?> </pre> -> copied all the values from it and pasted it into excel. Split the columns and summed it up in excel. Still didn’t get the same value as PHP array_sum

I’m sure it’s something stupid I’m missing here. If anyone can shed some light, that would be fantastic!

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@smohauthorMar 06.2011 — Found the problem finally. Such a stupid mistake! For those who are searching for the answer, if you have commas in your values, make sure you str_replace them.
×

Success!

Help @smoh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...