Search code examples
phparrayssum

Calculate sum of a column in a 2d array


I have the following array

    foreach ($reviews as $review){

$sqlreviews[] = array (

'score' => $review['value'] + $review['location'] + $review['service'] + $review['rooms'] + $review['cleanliness'],

);

The following echo outputs 11 25 25 25

foreach ($sqlreviews as $review1) { 
echo"
".$review1['score']."
";
}

How can I sum the above outputs and have the total of 86 to pass it to a variable?


Solution

  • $sum = 0;
    foreach ($sqlreviews as $review1) { 
        $sum += $review1['score'];
    }
    echo $sum;