How to print Array READABLE

北城余情 提交于 2020-01-16 00:45:36

问题


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.


来源:https://stackoverflow.com/questions/24895421/how-to-print-array-readable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!