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.

Share:
14,585
Ray
Author by

Ray

A coder with the passion of 'Never Stop Learning'.

Updated on November 21, 2022

Comments

  • Ray
    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
      Oliver Charlesworth over 12 years
      Do a var_dump immediately before you call array_sum...
    • Kieran
      Kieran over 12 years
      Not sure if this is necessary, but have you tried adding $VoteValue = array() before the loop?
    • Ray
      Ray over 12 years
      Thanks Oli and Kieran, got it sorted
  • Wrikken
    Wrikken over 12 years
    Would 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
    Ray over 12 years
    Hi 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
    Ray over 12 years
    Hi craigmj and Wrikken, thanks for the suggestions. Got it sorted.
  • Chrisvin Jem
    Chrisvin Jem almost 5 years
    Your answer provides no additional information or value to the answer already provided by craigmj and Pheonix.