How to print Array READABLE

2019-08-05 16:31发布

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?

7条回答
淡お忘
2楼-- · 2019-08-05 16:52

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.
查看更多
▲ chillily
3楼-- · 2019-08-05 16:58

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...
}
查看更多
成全新的幸福
4楼-- · 2019-08-05 17:02

I like:

highlight_string(var_export($array, true));

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

查看更多
三岁会撩人
5楼-- · 2019-08-05 17:08

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)

查看更多
我命由我不由天
6楼-- · 2019-08-05 17:10

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

echo "<pre>" . var_dump($array) . "</pre>";
查看更多
兄弟一词,经得起流年.
7楼-- · 2019-08-05 17:11
echo "<pre>".print_r($array, true)."</pre>";
查看更多
登录 后发表回答