Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 879 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to output (to a log) a multi-level array in a format that is human-readable?

#1
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:

<pre>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', ), '#post' => array ( 'form_wrapper' => array ( 'name' => '', 'pass' => '', ),</pre> ...

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).
Reply

#2
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`](php.net/manual/en/function.print-r.php) is best for readability but it doesn't print null/false values.

[`var_dump`](php.net/manual/en/function.var-dump.php) is best for checking types of values and lengths and null/false values.

[`var_export`](php.net/manual/en/function.var-export.php) 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.
Reply

#3
<del>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:

[To see links please register here]

;

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.

Reply

#4
Drupal's [Devel module][1] has other useful functions including ones that can print formatted arrays and objects to log files. See the guide at

[To see links please register here]


> **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.


[1]:

[To see links please register here]

Reply

#5

[To see links please register here]

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);`

[To see links please register here]



if you need to format it also in html, you can use `<pre>` tag
Reply

#6
This will help you

<strike>`echo '<pre>';`</strike>

$output = print_r($array,1);

<strike>`echo '</pre>';`</strike>

**EDIT**

using `echo '<pre>';` is useless, but `var_export($var);` will do the thing which you are expecting.
Reply

#7
**Syntax**

print_r(variable, return);


variable **Required**. Specifies the variable to return information about

return **Optional**. When set to true, this function will return the information (not print it). Default is false

**Example**

error_log( print_r(<array Variable>, TRUE) );
Reply

#8
I just wonder why nobody uses or recommends the way I prefer to debug an array:

<!-- language: lang-php -->

error_log(json_encode($array));

Next to my browser I `tail` my server log in the console eg.

<!-- language: lang-sh -->

tail -f /var/log/apache2/error.log

---

Though it's debatable if the output is human-readable, but it's still my preferred way to read it and would look something like that:

```php
[Tue Dec 13] [...] AH01071: Got error 'PHP message: {"form_wrapper":{"#tree":true,
"#type":"fieldset","#prefix":"","#suffix":"","#value":"","name":{"#type":
"textfield","#title":null,"#size":60,"#maxlength":60,"#required":false,
"#description":null,"#attributes":{"placeholder":"Email"},"#post":{
`"form_wrapper":{"name":"","pass":""}}}}}', referer:

[To see links please register here]

```
Reply

#9
If you need to log an error to Apache error log you can try this:

error_log( print_r($multidimensionalarray, true) );

> If you would like to capture the output of [print_r()][1], use the return parameter. When this parameter is set to true, print_r() will return the information rather than print it.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through