Function: Readable, traceable array dump

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Function: Readable, traceable array dump

Post by pickle »

This simple function will print out the file and line from which it was called, as well as dump in an orderly fashion the array it was passed. I've found this much more helpful than just calling print_r($someArray).

Code: Select all

function dump_array($p_array)
{
  $backtrace = debug_backtrace();
  $file = $backtrace[0]['file'];
  $line = $backtrace[0]['line'];
 
  echo '<pre>';
  echo "File: $file<br />";
  echo "Line: $line<br />";
  print_r($p_array);
  echo '</pre>';
}
Sample output:

Code: Select all

$ingredients = array('tomatoes','bacon','mayo','lettuce');
dump_array($ingredients);

Code: Select all

File: /path/to/my/file.php
Line: 2
Array
(
    [0] => tomatoes
    [1] => bacon
    [2] => mayo
    [3] => lettuce
)
 
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Finally got around to implementing this :)
Thanks for the snipplet ;)
Post Reply