Page 1 of 1

array_sum() returns zero

Posted: Mon Feb 20, 2012 1:32 pm
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,

Re: array_sum() returns zero

Posted: Mon Feb 20, 2012 2:12 pm
by tr0gd0rr
You forgot the [0]

Code: Select all

$Etos = array_sum($E_array[0]);

Re: array_sum() returns zero

Posted: Tue Feb 21, 2012 11:13 pm
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,

Re: array_sum() returns zero

Posted: Sun Feb 26, 2012 8:50 am
by thanga209
The [0] indicates the index of the first array.