daily pastebin goal
78%
SHARE
TWEET

PHP - Print array or object in readable form

lorro Mar 23rd, 2017 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.   // PHP - Print array or object in readable form
  3.   // replaces var_dump or print_r
  4.   if ( !function_exists( 'pretty_print' ) ) {
  5.     function pretty_print ( $item, $key = '', $indent = 0 ) {
  6.       $pad = str_repeat( '&nbsp;', $indent );
  7.       $type = gettype ($item );
  8.       print $pad.$type;
  9.       switch ($type) {
  10.         case 'array':
  11.         case 'object':
  12.           print ' '.$key.'<br>'.PHP_EOL;
  13.           $indent = $indent + 4;
  14.           foreach ($item as $key => $property) {
  15.             pretty_print( $property, $key, $indent );
  16.           }
  17.           break;
  18.         default:
  19.           print ' '.$key.' '.$item.'<br>';
  20.       }
  21.     }
  22.   }
RAW Paste Data
Top