Yes, I think the problem is that you’re creating a two-dimensional array in $rows, so trying to pass in $rows['rating'] to anything wouldn’t end well - $rows['rating'] would probably give an error if you just echoed it, you’d need to access it as $rows[0]['rating'] and so on, specifying the second dimension. Also, would you want to calculate the value of $avg every time you fetch a new record, as you are doing there?
You’ve either got to create a separate array for the rating and continue to use array_sum, or write your own function to sum the specific element you need. Or total the rating as you run through the loop, and maintain a row count, and do the division afterward. Several ways to achieve it really.
I was initially using the below inside my SQL query with a couple of INNER JOINS, though when I added this, things were not looping. I also read this can slow queries down and php was the best way of getting the avg result.
ROUND(AVG(r.rating), 1) AS ratingAVG
I’ve managed to reduce the value to 1 decimal place using: