Search code examples
phparraysdump

Non-archaic dumping of an array?


I usually use var_dump() to dump the contents of my array so as to assess the structure. However, this becomes tedious as with larger dumps, information spreads all across my 23" screen making it extremely hard to easily pick out which key matches which value.

Is there anyway to have the contents of an array dumped vertically, similar to the fashion which the PHP Manual uses:

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)

I'd really like to know, as this would be a great timesaver.

Any comments/suggestions/answers will be greatly appreciated ;)!!


Solution

  • If you want a non archaic debugging, var_dump, echo and other inline display of data are not the best way to go, xdebug and an IDE that supports debugging (netbeans, eclipse, aptana, ...) is.

    However, you can improve the readability of var_dump a lot by echoing a pre tag before it:

    function dump($var)
    {
       echo '<pre>';
       var_dump($var);
       echo '</pre>';
    }
    

    (note that you can get the same sort of results by showing the source of the page instead of the html rendering)