Your code uses this:
number_format((float)$results[$i]['avg_rate'], 2, '.', '')
And if we look at where $i is set, we see this just before the loop:
But I don't see how this maps in any way to the current post when there's a conditional, meaning that not everything in the results array is included in the post query.