How to output (to a log) a multilevel array in a format that is readable by humans?

I am working on a drupal site and when debugging, I always have to read long nested arrays. As a result, most of my life is spent using arrows, return and tab keys to break 1000+ character strings into an embedded, readable format.

For drupal developers, I cannot use devel dsm (), since I work with multi-step forms # ahah / # ajax, and I can only output arrays to the error log and not to the screen.

Illustrative 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', ), 

Change Sorry, I didn’t mean β€œdo not display” through drupal system messages, where arrays can be displayed in an interactive, nested format (using devel.module).

+65
php drupal
Aug 09 2018-12-12T00:
source share
6 answers

If you need to log an error in the Apache error log, you can try the following:

 error_log( print_r($multidimensionalarray, TRUE) ); 
+128
Sep 11 '13 at 12:48 on
source share

http://php.net/manual/en/function.print-r.php This function can be used to format the output,

 $output = print_r($array,1); 

$output is a string variable, it can be registered like any 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 the <pre>

+22
Aug 09 2018-12-12T00:
source share

Simple material:

Using print_r , var_dump or var_export should do it pretty nicely if you look at the result in view mode not in HTML mode or as @Joel Larson said if you wrap everything in a <pre> tag.

print_r best for reading, but it does not display null / false values.

var_dump best to check for value types and lengths and null / false values.

var_export is close to var_dump , but it can be used to get a reset string.

The format returned by any of them is correctly printed in the source code, and var_export can be used for logging, since it can be used to return a discarded string.

Advanced Material:

Use the xdebug plugin for PHP, which prints var_dump as strings in HTML format and not as a raw dump format, and also allows you to create a custom function that you want to use for formatting.

+5
Aug 09 2018-12-12T00:
source share

Drupal The Devel module contains other useful features, including those that can print formatted arrays and objects in log files. See the manual at http://ratatosk.net/drupal/tutorials/debugging-drupal.html

dd ()

Writes any variable to a file named "drupal_debug.txt" in temp sites. All the output from this function is added to the log file, which makes it easier to view the contents of the change variable when the code changes.

If you are using Mac OS X, you can use the Logging Console to monitor the contents of the log file.

If you use a Linux flavor, you can use the tail -f drupal_debug.txt command to view the data written to the file.

+2
Aug 09 2018-12-12T00:
source share

It will help you

echo '<pre>'; hit>

 $output = print_r($array,1); 

echo '</pre>'; hit>

EDIT

using echo '<pre>'; useless, but var_export($var); will accomplish what you expect.

+1
Aug 09 2018-12-12T00:
source share

You should be able to use var_dump () in the pre tag. Otherwise, you can study a library like dump_r.php: https://github.com/leeoniya/dump_r.php

My decision is wrong. The OP was looking for a solution formatted with spaces for storage in a log file.

The solution could be to use output buffering with var_dump, then str_replace () all the tabs with spaces for formatting it in the log file.

0
Aug 09 2018-12-12T00:
source share



All Articles