array_sum() expects parameter 1 to be array
14,585
Solution 1
Hmm, weird, i dont know what the error is in your code, but try this out :
$VoteValue = array();
for ($i=0; $i<=30; $i++) {
$VoteValue[] = $i;
}
$Total = array_sum($VoteValue);
echo "<br /><br />Total Vote = $Total <br />";
Solution 2
Have you tried initializing $VoteValue with
$VoteValue = array();
?
As a total aside, the sum from 0 to n = ( n * (n+1) ) / 2.
Solution 3
try this way. I hope to be helpful
$VoteValue = [];
for ($i=0; $i<=30; $i++) {
array_push($VoteValue, $i);
}
$Total = array_sum($VoteValue);
echo "<br /><br />Total Vote = $Total <br />";
Solution 4
i'v tried,on the ubuntu11.04 with lampp. no errors.
Total Vote = 465
maybe the php problem.
Comments
-
Ray over 1 year
Need someone to help me to crack the following.
On my local machine, i'm running MAMP on my Mac. I have the following code:
$x=0; for ($i=0; $i<=30; $i++) { $VoteValue[$x] = $i; $x++; } $Total = array_sum($VoteValue); echo "<br /><br />Total Vote = $Total <br />";
The above code works and run fine on my local machine. But, when I uploaded it to my production server, I get the following warning prompts:
Warning: array_sum() expects parameter 1 to be array, null given in /home/RIP/public_html/RIPVote.php on line 7
and it doesn't return and show the output value due to the warning error. How do i get rid of this?
Please advice and looking forward to hear from you guys soon. Appreciate and thanks.
-
Oliver Charlesworth over 12 yearsDo a
var_dump
immediately before you callarray_sum
... -
Kieran over 12 yearsNot sure if this is necessary, but have you tried adding
$VoteValue = array()
before the loop? -
Ray over 12 yearsThanks Oli and Kieran, got it sorted
-
-
Wrikken over 12 yearsWould work indeed if
$VoteValue
is a string before, short explanation:$VoteValue[n]=$something
would then replace the n-th character with the first character of$something
, and just remain an (altered) string. -
Ray over 12 yearsHi Pheonix, Thanks for your precise solution and example. It works now. Much appreciated really. Also, thanks for rest for the examples as well as i have tried it.
-
Ray over 12 yearsHi craigmj and Wrikken, thanks for the suggestions. Got it sorted.
-
Chrisvin Jem almost 5 yearsYour answer provides no additional information or value to the answer already provided by craigmj and Pheonix.