Warning: Cannot use a scalar value as an array function theme_test($form) {

  $output = '';

  foreach($form['field'] as $key =&gt; $val){

    $output .= '&lt;div class="test"&gt;';
    $output .= drupal_render($val['uid'. $key]);
    $output .= '&lt;/div&gt;';
    unset($key,$val);
  }

  return $output;

}


When I run this code, I get the following warning message:


  Warning: Cannot use a scalar value as an array in theme_test()


How do I resolve this, and what is the reason behind this warning?
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.