my code:
foreach($comment as $key => $value) {
$total = $value['likes'];
echo "$key: $total\n"
}
Outputs:
0: 3 1: 18 2: 72 3: 0 4: 10 5: 0 6: 0 7: 0 8: 0 9: 0 10: 0 11: 19 12: 0 13: 0 14: 14 15: 19 16: 0 17: 0 18: 1
How do I sum all the values? The array_sum function gives a “warning: expects parameter 1 to be array, integer given”.
Array_sum is expecting a single dimension array. From the looks of your code you have a multi-dimensional array. Where $value is an array too.
You have the right idea with the total, but you have to add to your total like this
$total = $total + $value['likes'], or the shorthanded version$total += $value['likes']: