php 如何以人类可读的格式输出(到日志)多级数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11884891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to output (to a log) a multi-level array in a format that is human-readable?
提问by Mark Shiraldi
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.
我在一个 Drupal 站点上工作,在调试时,我总是不得不通读长长的嵌套数组。结果,我生命中的大部分时间都在使用箭头键、返回键和 Tab 键,将 1000 多个字符串拆分为嵌套的、可读的格式。
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.
对于 drupal 开发人员,我不能使用 devel 的 dsm(),因为我正在使用多步 #ahah/#ajax 表单,而且我只能将数组输出到错误日志,而不是屏幕。
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).
编辑:抱歉,“不输出到屏幕”是指通过 drupal 的系统消息,可以在其中以可点击的嵌套格式(使用 devel.module)输出数组。
回答by Akhilraj N S
If you need to log an error to Apache error log you can try this:
如果您需要将错误记录到 Apache 错误日志中,您可以尝试以下操作:
error_log( print_r($multidimensionalarray, TRUE) );
回答by Fivell
http://php.net/manual/en/function.print-r.phpThis function can be used to format output,
http://php.net/manual/en/function.print-r.php这个函数可以用来格式化输出,
$output = print_r($array,1);
$outputis a string variable, it can be logged like every other string. In pure php you can use trigger_error
$output是一个字符串变量,它可以像其他每个字符串一样被记录。在纯 php 中,您可以使用trigger_error
Ex. trigger_error($output);
前任。 trigger_error($output);
http://php.net/manual/en/function.trigger-error.php
http://php.net/manual/en/function.trigger-error.php
if you need to format it also in html, you can use <pre>tag
如果您还需要将其格式化为 html,则可以使用<pre>标签
回答by Mihai Stancu
Simple stuff:
简单的东西:
Using print_r, var_dumpor var_exportshould 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,var_dump或者var_export如果您在视图源模式下而不是在 HTML 模式下查看结果,或者如@Joel Larson 所说,如果您将所有内容都包装在一个<pre>标签中,那么它应该做得很好。
print_ris best for readability but it doesn't print null/false values.
print_r最适合可读性,但它不打印空/假值。
var_dumpis best for checking types of values and lengths and null/false values.
var_dump最适合检查值的类型和长度以及空/假值。
var_exportis simmilar to var_dumpbut it can be used to get the dumped string.
var_export类似于var_dump但可用于获取转储的字符串。
The format returned by any of these is indented correctly in the source code and var_exportcan be used for logging since it can be used to return the dumped string.
其中任何一个返回的格式都在源代码中正确缩进,var_export可用于日志记录,因为它可用于返回转储的字符串。
Advanced stuff:
进阶内容:
Use the xdebug plug-in for PHP this prints var_dumps as HTML formatted strings not as raw dump format and also allows you to supply a custom function you want to use for formatting.
使用 PHP 的 xdebug 插件,它将var_dumps打印为 HTML 格式的字符串而不是原始转储格式,并且还允许您提供要用于格式化的自定义函数。
回答by nmc
Drupal's Devel modulehas 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
Drupal 的Devel 模块还有其他有用的功能,包括可以将格式化的数组和对象打印到日志文件的功能。请参阅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.
dd()
将任何变量记录到站点临时目录中名为“drupal_debug.txt”的文件中。此函数的所有输出都附加到日志文件中,以便在修改代码时轻松查看变量的内容如何更改。
如果您使用的是 Mac OS X,您可以使用日志控制台来监控日志文件的内容。
如果您使用的是 Linux 版本,您可以使用命令“tail -f drupal_debug.txt”来查看记录到文件中的数据。
回答by code-jaff
This will help you
这会帮助你
echo '<pre>';
echo '<pre>';
$output = print_r($array,1);
echo '</pre>';
echo '</pre>';
EDIT
编辑
using echo '<pre>';is useless, but var_export($var);will do the thing which you are expecting.
使用echo '<pre>';是没用的,但var_export($var);会做你期待的事情。
回答by Joel Larson
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
您应该能够在 pre 标签中使用 var_dump() 。否则你可以考虑使用像 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.
我的解决方案不正确。OP 正在寻找一种用空格格式化的解决方案以存储在日志文件中。
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.
一种解决方案可能是将输出缓冲与 var_dump 一起使用,然后 str_replace() 所有带有空格的选项卡以在日志文件中对其进行格式化。
回答by wittich
I just wonder why nobody uses or recommends the way I prefer to debug an array:
我只是想知道为什么没有人使用或推荐我更喜欢调试数组的方式:
error_log(json_encode($array));
Next to my browser I tailmy server log in the console eg.
在我的浏览器旁边,我的tail服务器登录控制台,例如。
tail -f /var/log/apache2/error.log

