How to print Array READABLE

2019-08-05 16:46发布

问题:

How do i print an Array readable? print_r($array); and var_dump($array); both produce some very ugly clutter of that $array. Ok it is what it says, it prints that array, but i'd like to have some well formated print of that array, how do i do that?

回答1:

Actually, it is well-formatted but HTML ignores line breaks and double spaces.

You just have to view the page's source code (CTRL+U or right-click > view source code)



回答2:

echo '<pre>';
var_dump($array);
echo '</pre>';

Or to a better yet performance, use xDebug with html_colors = on. html_colors can be found on php.ini file. xdebug is from http://xdebug.org/download.php

With xDebug and Colors you don't need to use

echo '<pre>';
echo '</pre>';

it's beautiful as-is.



回答3:

You can wrap your var_dump() output in <pre> to preserve the monospacing:

echo "<pre>" . var_dump($array) . "</pre>";


回答4:

echo "<pre>".print_r($array, true)."</pre>";


回答5:

try this code

$myArray=array("a","b","c");

foreach($myArray as $A)
{
echo $A."<br/>"; // you can use any style here like table, div span etc...
}


回答6:

I like:

highlight_string(var_export($array, true));

var_export() is usable (copy/paste) PHP code as well.



回答7:

if you want to see it well formatted, you can encode it to json object and print it pretty :)

You have to do is:

echo json_encode($array,JSON_PRETTY_PRINT); //This needs PHP 5.3 at least.