I can't tell what's wrong with your code, but chances are you're going to want to restructure the way you handle data coming out of the database. Right now you're kind of mashing together a couple different suggestions and it may not be the best - you'll want to re-write it cleanly, in my opinion.
It could be your use of min(array_values() and max(array_values(), seeing as we've changed the initial array it's referencing. We added a sub-array, so you'll want to adjust for that, I think.