如何以人类可读的格式输出(到日志)多级数组?
I'm working on a drupal site and when debugging, I am always having to read through long, nested arrays. As a result, a large portion of my life is spent using the arrow, return, and tab keys, to split up 1000+ character strings into a nested, readable format.
For drupal devs, I can't use devel's dsm(), as I'm working with multi-step #ahah/#ajax forms, and I can only output the arrays to the error log, not to the screen.
Visual example:
Evil:
array ( 'form_wrapper' => array ( '#tree' => true, '#type' => 'fieldset', '#prefix' => '', '#suffix' => '', '#value' => '', 'name' => array ( '#type' => 'textfield', '#title' => NULL, '#size' => 60, '#maxlength' => 60, '#required' => false, '#description' => NULL, '#attributes' => array ( 'placeholder' => 'Email', ), '#post' => array ( 'form_wrapper' => array ( 'name' => '', 'pass' => '', ),...
Good:
array (
'form_wrapper' => array (
'#tree' => true,
'#type' => 'fieldset',
'#prefix' => '<div>',
'#suffix' => '</div>',
'#value' => '',
'name' => array (
'#type' => 'textfield',
'#title' => NULL,
'#size' => 60,
'#maxlength' => 60,
'#required' => false,
'#description' => NULL,
'#attributes' => array (
'placeholder' => 'Email',
),
Edit: Sorry, by "not output to screen", I meant via drupal's system messages where it's possible to output arrays in a clickable, nested format (using devel.module).
If you need to log an error to Apache error log you can try this:
error_log( print_r($multidimensionalarray, TRUE) );
You should be able to use a var_dump() within a pre tag. Otherwise you could look into using a library like dump_r.php: https://github.com/leeoniya/dump_r.php
My solution is incorrect. OP was looking for a solution formatted with spaces to store in a log file.
A solution might be to use output buffering with var_dump, then str_replace() all the tabs with spaces to format it in the log file.
Simple stuff:
Using print_r
, var_dump
or var_export
should do it pretty nicely if you look at the result in view-source mode not in HTML mode or as @Joel Larson said if you wrap everything in a <pre>
tag.
print_r
is best for readability but it doesn't print null/false values.
var_dump
is best for checking types of values and lengths and null/false values.
var_export
is simmilar to var_dump
but it can be used to get the dumped string.
The format returned by any of these is indented correctly in the source code and var_export
can be used for logging since it can be used to return the dumped string.
Advanced stuff:
Use the xdebug plug-in for PHP this prints var_dump
s as HTML formatted strings not as raw dump format and also allows you to supply a custom function you want to use for formatting.
http://php.net/manual/en/function.print-r.php This function can be used to format output,
$output = print_r($array,1);
$output
is a string variable, it can be logged like every other string. In pure php you can use trigger_error
Ex. trigger_error($output);
http://php.net/manual/en/function.trigger-error.php
if you need to format it also in html, you can use <pre>
tag
Drupal's Devel module has other useful functions including ones that can print formatted arrays and objects to log files. See the guide at http://ratatosk.net/drupal/tutorials/debugging-drupal.html
dd()
Logs any variable to a file named “drupal_debug.txt” in the site’s temp directory. All output from this function is appended to the log file, making it easy to see how the contents of a variable change as you modify your code.
If you’re using Mac OS X you can use the Logging Console to monitor the contents of the log file.
If you’re using a flavor of Linux you can use the command “tail -f drupal_debug.txt” to watch the data being logged to the file.
This will help you
echo '<pre>';
$output = print_r($array,1);
echo '</pre>';
EDIT
using echo '<pre>';
is useless, but var_export($var);
will do the thing which you are expecting.