How to pass an array or an object from a module to a theme I have used hook_menu() with a page callback as follows:

function profilecb_menu() {
  $items['profilecb'] = array(
    'title' =&gt; 'Profile CB',
    'page callback' =&gt; 'pcb_exe',
    'access callback' =&gt; TRUE,
    'type' =&gt; MENU_CALLBACK,
  );

  return $items;
}


The callback code is the following:

function pcb_exe($id) {
  $ar = array('arg1'=&gt; 'value1', 'arg2'=&gt; $id);

  return $ar;
}


I have been able to make the callback function return a single string; when I try to return an array, I get an error.

All I need to do is to make the callback function do some tasks, like connecting to the database, and return to a template a complex array of useful information.

How can I do that?
In Drupal 6, a menu callback needs to return a string that contains the HTML to output together the output of other modules; alternatively, it can print the output and call exit().

If you need to pass an array to a theme function, then the output returned from the menu callback should be output from a theme function to which the menu callback passes an array.

function pcb_exe($id) {
  $ar = array('arg1'=&gt; 'value1', 'arg2'=&gt; $id);
  return theme('pcb_exe_output', $ar);
}


In Drupal 7 (and higher versions), the output of a menu callback is normally an array that has the format used by drupal_render().
It still possible to pass an array to a theme function, but the parameters passed to a theme function changed.

function pcb_exe($id) {
  $ar = array('arg1'=&gt; 'value1', 'arg2'=&gt; $id);
  return theme('pcb_exe_output', $ar);
}


In this case, there is no noticeable difference, as the parameter passed to the theme function was already an array.
The parameter passed to a theme function since Drupal 7 is an array containing all the values required from the theme function. See the code of theme_vertical_tabs() as example:

function theme_vertical_tabs($variables) {
  $element = $variables['element'];
  // Add required JavaScript and Stylesheet.
  drupal_add_library('system', 'drupal.vertical-tabs');

  $output = '&lt;h2 class="element-invisible"&gt;' . t('Vertical Tabs') . '&lt;/h2&gt;';
  $output .= '&lt;div class="vertical-tabs-panes"&gt;' . $element['#children'] . '&lt;/div&gt;';
  return $output;
}