1

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?

cli
  • 162
  • 2
  • 10

7 Answers7

3
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.

0

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

echo "<pre>" . var_dump($array) . "</pre>";
esqew
  • 42,425
  • 27
  • 92
  • 132
0
echo "<pre>".print_r($array, true)."</pre>";
Zombiesplat
  • 943
  • 8
  • 19
0

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...
}
Kalpesh Rajai
  • 2,040
  • 27
  • 39
0

I like:

highlight_string(var_export($array, true));

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

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

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)

NiloVelez
  • 3,611
  • 1
  • 22
  • 30
0

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.
user2854865
  • 65
  • 1
  • 3
  • This doesn't work for me. PHP Version 5.3.26 running. Notice: Use of undefined constant JSON_PRETTY_PRINT - assumed 'JSON_PRETTY_PRINT' in /srv/www/htdocs/web682/html/cal/functions.php on line 77 Warning: json_encode() expects parameter 2 to be long, string given in /srv/www/htdocs/web682/html/cal/functions.php on line 77 – cli Jul 26 '14 at 22:45