function theme_test($form) {

  $output = '';

  foreach($form['field'] as $key => $val){

    $output .= '<div class="test">';
    $output .= drupal_render($val['uid'. $key]);
    $output .= '</div>';

  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?

  • You should add more information, especially why you expect $val to be an array. As it is, it's more a question about PHP, and not a question specific for Drupal. The meaning of the warning doesn't change, in Drupal. – kiamlaluno Sep 23 '11 at 11:57

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


Looking at your code again, this line looks suspect:


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.

  • That is true: Using unset($key,$val); is completely useless; it would be like unsetting the local variables used in a function before it returns the control back to the caller function. – kiamlaluno Sep 23 '11 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.