PHP 打印数组,每行一个键,这样对眼睛更容易吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8541026/
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
PHP print array with one key per line so it's easier on the eyes?
提问by lisovaccaro
How can I print an array so instead of being all a single line such as:
如何打印一个数组而不是全部为一行,例如:
Array ( [0] => Array ( [Palabra] => correr [Tipo] => Verbo ) [1] => Array ( [Palabra] => es [Tipo] => Verbo [PersonaSujeto] => tercera [CantidadSujeto] => singular [Tiempo] => presente )
It displays something more readable like this or similar:
它显示了一些更具可读性的内容,例如这样或类似的内容:
Array (
[0] => Array (
[Palabra] => correr
[1] => Array (
[Tipo] => Verbo
[Tiempo] => Infinitivo) )
[1] => Array (
[Palabra] => es
[Tipo] => Verbo
[PersonaSujeto] => tercera
[CantidadSujeto] => singular
[Tiempo] => presente )
回答by Madara's Ghost
It is printed out with a line break, but in HTML line breaks are meaningless. You can do one of 2 things:
它是用换行符打印出来的,但在 HTML 中换行符是没有意义的。您可以做以下两件事之一:
Tell the browser it's not an HTML document, but a text, by placing the following code before any output is sent:
header('Content-type: text/plain');
Simply have
<pre>
tags around the printed array:echo '<pre>'; print_r($array); echo '</pre>';
通过在发送任何输出之前放置以下代码,告诉浏览器它不是 HTML 文档,而是文本:
header('Content-type: text/plain');
只需
<pre>
在打印数组周围放置标签:echo '<pre>'; print_r($array); echo '</pre>';
回答by FDisk
I love code highlighting. This example displays the beatified array with highlighted code
我喜欢代码高亮。此示例显示带有突出显示代码的优化数组
<?php
$debug_data = array(1,2,5,6,8,7,'aaa');
echo str_replace(array('<?php ','?>'), '', highlight_string( '<?php ' . var_export($debug_data, true) . ' ?>', true ) );
?>
回答by Chris Baker
If you wrap the output of functions like print_r
, var_dump
, and var_export
in a <pre>
tag, it will be relatively nicely formatted.
如果您将print_r
, var_dump
, 和等函数的输出包装var_export
在一个<pre>
标签中,它的格式会相对较好。
The reason is because the output of the functions is plain text, but when you look at it in a browser, the browser considers it HTML. The new line characters in the plaintext output are not meaningful to HTML, as new lines are ignored.
原因是函数的输出是纯文本,但是当您在浏览器中查看时,浏览器会将其视为 HTML。纯文本输出中的换行符对 HTML 没有意义,因为新行被忽略。
To see this in action, try viewing the source - you'll see the nicely formatted output there.
要查看此操作,请尝试查看源代码 - 您将在那里看到格式良好的输出。
The pre
HTML tag tells a browser "everything inside of this block is pre-formated". New lines are treated as new lines, spacing is respected (HTML also doesn't care about sequences of spaces).
该pre
HTML标记,告诉浏览器“这个块里面的东西是预-formated”。新行被视为新行,尊重间距(HTML 也不关心空格序列)。
So, you're left with something like this:
所以,你只剩下这样的事情:
echo '<pre>'.print_r($my_array).'</pre>';
Instead of doing that all over my code, I like to use a composite function like this (I call it print_p
so it is liketyping print_r
)
而不是这样做,都在我的代码,我喜欢用这样的复合函数(我把它叫做print_p
所以很喜欢打字print_r
)
function print_p($value = false, $exit = false, $return=false, $recurse=false) {
if ($return === true && $exit === true)
$return = false;
$tab = str_repeat(" ", 8);
if ($recurse == false) {
$recurse = 0;
$output = '<div style="width:100%; border: 2px dotted red; background-color: #fbffd6; display: block; padding: 4px;">';
$backtrace = debug_backtrace();
$output .= '<b>Line: </b>'.$backtrace[0]['line'].'<br>';
$output .= '<b>File: </b> '.$backtrace[0]['file'].'<br>';
$indent = "";
} else {
$output = '';
$indent = str_repeat(" ", $recurse * 8);
}
if (is_array($value)) {
if ($recurse == false) {
$output .= '<b>Type: </b> Array<br>';
$output .= "<br>array (<br>";
} else {
$output .= "array (<br>";
}
$items = array();
foreach ($value as $k=>$v) {
if (is_object($v) || is_array($v))
$items[] = $indent.$tab."'".$k."'=>".print_p($v, false, true, ($recurse+1));
else
$items[] = $indent.$tab."'".$k."'=>".($v===null ? "NULL" : "'".$v."'");
}
$output .= implode(',<br>', $items);
if ($recurse == false)
$output .= '<br>)';
else
$output .= '<br>'.$indent.')';
} elseif (is_object($value)) {
if ($recurse == false) {
$output .= '<b>Type: </b> Object<br>';
$output .= '<br>object ('.get_class($value).'){'."<br>";
} else {
$output .= "object (".get_class($value)."){<br>";
}
// needed conditional because base class function dump is protected
$vars = get_object_vars($value);
$vars = (is_array($vars) == true ? $vars : array());
$items = array();
foreach ($vars as $k=>$v) {
if (is_object($v) || is_array($v))
$items[] = $indent.$tab."'".$k."'=>".print_p($v, false, true, ($recurse+1));
else
$items[] = $indent.$tab."'".$k."'=>".($v===null ? "NULL" : "'".$v."'");
}
$output .= implode(',<br>', $items);
$vars = get_class_methods($value);
$items = array();
foreach ($vars as $v) {
$items[] = $indent.$tab.$tab.$v;
}
$output .= '<br>'.$indent.$tab.'<b>Methods</b><br>'.implode(',<br>', $items);
if ($recurse == false)
$output .= '<br>}';
else
$output .= '<br>'.$indent.'}';
} else {
if ($recurse == false) {
$output .= '<b>Type: </b> '.gettype($value).'<br>';
$output .= '<b>Value: </b> '.$value;
} else {
$output .= '('.gettype($value).') '.$value;
}
}
if ($recurse == false)
$output .= '</div>';
if ($return === false)
echo $output;
if ($exit === true)
die();
return $output;
}
... then you do this:
...然后你这样做:
print_p($my_array);
...and get the output:
...并获得输出:
This is nice because it a) will take any type of variable, objects, arrays, strings, and b) tell you where the output is coming from. It can get really frustrating if you lose track of where you had put a debugging message and have to spend time searching all over for it! :)
这很好,因为它 a) 将采用任何类型的变量、对象、数组、字符串,并且 b) 告诉您输出的来源。如果您忘记了放置调试消息的位置并且不得不花时间到处寻找它,那会非常令人沮丧!:)
回答by Serdar De?irmenci
This is the best friend of a PHP programmer:
这是 PHP 程序员最好的朋友:
function pa($value, $exit = true){
echo "<pre>";
print_r($value);
echo "</pre>";
if($exit){
exit();
}
}
If you need use like this:
如果你需要这样使用:
pa($arr);
or
或者
pa($obj);
If you don't want to exit
如果不想退出
pa($obj, false);
回答by SlavaNov
Add <pre>
添加 <pre>
Example:
例子:
<pre>
<?php
print_r($array);
?>
<pre>
回答by RiaD
As other answers say: There are already newlines and tabs in the
print_r
output. And you can see it using<pre>
or seeing as plain text.You may install
xdebug
to make output ofprint_r
more readable
正如其他答案所说:
print_r
输出中已经有换行符和制表符。您可以使用<pre>
或以纯文本形式查看它。您可以安装
xdebug
以使输出print_r
更具可读性
回答by joshdutcher
All the above answers are good. I like to include a debug()
php function in my functions so anywhere in my app I can just call debug($my_array);
to dump the array to the screen (or the html), with some nice readability formatting. Here it is on github.
以上所有答案都很好。我喜欢debug()
在我的函数中包含一个php 函数,所以我可以在我的应用程序的任何地方调用debug($my_array);
以将数组转储到屏幕(或 html),并具有一些很好的可读性格式。这是在 github 上。
Of course these days most servers and frameworks have their own built-in version of something like this, but for building something from scratch this works.
当然,现在大多数服务器和框架都有自己的内置版本,但对于从头开始构建的东西,这是可行的。