Warning: Cannot use a scalar value as an array
There's somehting funky happening on this line:

$output .= drupal_render($val['uid'. $key]);


$val is not an array, you'll need to rethink your PHP code. Do a Google search for "Warning: Cannot use a scalar value as an array" and you'll see a bunch of cases where other users are dealing with the same issue. As an example:

PHP : Cannot use a scalar value as an array

EDIT:

Looking at your code again, this line looks suspect:

unset($key,$val);


You're in a loop that is using these values. Applying an unset could result in the issue in addition to the point I made above.