-1
I’m storing an array in a Session, but I’m not able to manipulate it. How do I add all the values of the array?
I tried this way:
$item = $_SESSION['item'];
$total = 0;
foreach($item as $x) {
$total += $x;
}
echo $total;
exit();
however, the return is:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\quiz_Acaro\final.php on line 70
0
Can do
var_dump($item)
in the second line and post the result?– Woss
@Andersoncarloswoss 23string(2) "23"
– Joao Pedro
So there’s a string in your session, not a array. How you set the session?
– Woss
@Andersoncarloswoss is a step by step. in the first part, the user has checkboxes to mark, and sends the values via post. sending via post, var_dump($item) returns the array, not string. so, probably the error is from step 2 to step 3 (there are 3 steps)
– Joao Pedro
@Andersoncarloswoss array(2) { [0]=> string(1) "2" [1]=> string(1) "3" }
– Joao Pedro
Then ask the question your complete code, especially the code referring to steps 2 and 3, whatever that means.
– Woss
I got it. Thanks for helping :D
– Joao Pedro
How did you get it? Were you setting the session wrong? What was the error?
– Woss
@Andersoncarloswoss in step 2 I was turning the array into string at the time of sending to step 3
– Joao Pedro
Then put the full code in the question and then answer how you solved the problem. In my view, the error was in the definition of the session, not in the reading, so the answers given, even correct, would not solve the problem described.
– Woss