array_sum() returns zero

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yoda69
Forum Newbie
Posts: 16
Joined: Wed Jun 20, 2007 10:21 am

array_sum() returns zero

Post by yoda69 »

Hey all,
I getting an array from mysql and trying to sum the results. The function always return zero. What am I doing wrong?

Code: Select all

include "conn.php";
$query1 = "SELECT q1, q3, q7, q10, q13, q14, q19, q22, q25, q27, q30, q33, q35, q36 FROM shiva WHERE subjectId = ".$subject."";
$result1 = mysql_query($query1) or die(mysql_error());
while($row1=mysql_fetch_assoc($result1)) 
{ 
$E_array[] = $row1;
$Etos = array_sum($E_array);

btw. the array from the mysql looks this:

Array ( [0] => Array ( [q1] => 1 [q3] => 1 [q7] => 0 [q10] => 0 [q13] => 0 [q14] => 0 [q19] => 1 [q22] => 0 [q25] => 0 [q27] => 1 [q30] => 0 [q33] => 1 [q35] => 1 [q36] => 0 ) )

Thanks,
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: array_sum() returns zero

Post by tr0gd0rr »

You forgot the [0]

Code: Select all

$Etos = array_sum($E_array[0]);
yoda69
Forum Newbie
Posts: 16
Joined: Wed Jun 20, 2007 10:21 am

Re: array_sum() returns zero

Post by yoda69 »

Hmm....
I couldn't find any reference for that in php manual. Can someone explain what is the function of the [0] in this case.
Thanks so much,
thanga209
Forum Newbie
Posts: 2
Joined: Mon Feb 21, 2011 5:43 am

Re: array_sum() returns zero

Post by thanga209 »

The [0] indicates the index of the first array.
Post Reply