Hi guys,

I have a question on the print_r() method. I know that we can used this function to print out an array. My problem is if i got too many elements in the array, the print_r() could not return a nice look print out. For example:
print_r($array) may have result of:

Array ( [514] => stdClass Object ( [cookie_id] => 1 [nid] => 514 [qty] => 2 [changed] => 1183484115 [data] => 7362 [vid] => 514 [pparent] => 0 [sku] => [price] => 342.55 [ptype] => tangible [hide_cart_link] => 0 [stock] => 7 [manage_stock] => 1 [availability] => 1 [original_price] => 527.00 )

but what i want is something looks like this:

Array(
[514] => stdClass Object (
                                   [cookie_id] => 1
                                   [nid] => 514
                                   [qty] => 2...
                      )

[data]=> 7362

...

Anyone have clue how to do it?
Thanks.



Comments

robmilne’s picture

echo '<pre>'; print_r($array); echo '</pre>';

mandclu’s picture

If you view the source of the page, you should be able to see structured version of the output, which is probably what you want.

If it absolutely has to appear formatted in the standard browser view of the page, you can try a couple of things, like:

<pre>
<?php print_r($array_var); ?>
</pre>

Or, you could use the dprint_r function in the devel module, though that's probably overkill for just the one function. Still, it does a nice job of formatting the output.

rszrama’s picture

And just one more tip... I use the following all the time in my modules to debug different parts:

  drupal_set_message('<pre>'. print_r($array_or_object_var, TRUE) .'</pre>');

I use it so much I should create a keyboard key just for that string. : P

----------------------
Current Drupal project: http://www.ubercart.org

ardee-1’s picture

Great tip rszrama! Thanks!

nancydru’s picture

// Helper function for traversing an array.
// Since some arrays may contain arrays, this function can be called recursively.
// Also handles objects.
function _handle_array(&$array) {
  $header = array(t('Name'), t('Value'));
  $rows = array();

  if (count($array) == 0) { return t('Empty'); }

  foreach ($array as $name => $value) {
    if (is_array($array[$name])) { $value = _handle_array($array[$name]); }  /* recursive call for imbedded array */
    else { 
      if (is_string($array[$name]) || is_integer($array[$name]) || is_null($array[$name])) { $value = check_plain($array[$name]); }
      else {
        if (is_bool($array[$name])) { $value = $array[$name] ? t('True') : t('False'); }
        else { 
          if (is_object($array[$name])) {
            $obj = array();
            foreach ($array[$name] as $key => $val) {
              if (is_array($val)) { $val = _handle_array($val); }
               $obj[] = "[$key] => $val";
             } /* end foreach obj */
            $value = theme('item_list', $obj);
           } /* end if obj */
          else {
            $value = '<strong>'. gettype($array[$name]) .'</strong>';
            drupal_set_message('Help, I found a '. gettype($array[$name]) .' in the data!', 'warning');
           } /* end else obj */
        } /* end else bool */
       } /* end else string */
     } /* end else array */
    $rows[] = array($name, $value);
   } /* end foreach */

  return theme('table', $header, $rows);
}

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

edex13’s picture

hi,
can you explain how to use that function?

thanks

nancydru’s picture

It goes into a module. Then you call it with the name of an array. It will produce a table showing the values recursed down to the bottom.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database