Search code examples
phppretty-print

How can I format (pretty print) a multi-dimensional array for debugging?


I've seen some online pretty print modules for code. Anyone know of one that will format a multi-dimensional array into readable display?

Example, translate this:

array(83) { [0]=> array(2) { ["name"]=> string(11) "CE2 Options" ["type"]=> string(5) "title" } [1]=> array(1) { ["type"]=> string(4) "open" } [2]=> array(5) { ["name"]=> string(8) "Template" ["desc"]=> string(638) "test description" ["id"]=> string(9) "my_theme" ["type"]=> string(14) "selectTemplate" ["options"]=> array(13) {

Into this...

array(83) { 
    [0]=> array(2) { ["name"]=> string(11) "My Options" ["type"]=> string(5) "title" } 
    [1]=> array(1) { ["type"]=> string(4) "open" } 
    [2]=> array(5) { 
        ["name"]=> string(8) "Template" 
        ["desc"]=> string(638) "Test description" 
        ["id"]=> string(9) "my_theme" 
        ["type"]=> string(14) "selectTemplate" 
        ["options"]=> array(13) { 
            [0]=> string(10) "test" 

Solution

  • If you want a nicer output than var_dump , then check out the alternatives listed here:
    A more pretty/informative Var_dump alternative in PHP?

    Particularily http://krumo.sourceforge.net/ provides a much more accessible DHTML view for variable dumps. (It requires an extra include() though.)

    And if you actually want to keep the generated output as static html, you might have to write a smallish wrapper script.