George
George

Reputation: 2493

How do I dump arrays in a nice orderly fashion?

Whenever I use print_r or var_dump they come out all sloppy and in one line instead of formatted like I see so many people and the actual php.net site being able to achieve. What do I do to get them like this -

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

Instead of this

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

(Pretty sure it comes out even messier than that I was just deleting whitespace by hand.)

Upvotes: 0

Views: 170

Answers (2)

SpYk3HH
SpYk3HH

Reputation: 22580

If you really want a simple function, that keeps arrays pretty, can take infinite params, and doesn't require a full library, try my snippet below. It will print everything from array's to xml objects within pre tags with headers providing info of the parameter type and possible object class.

function preDump() {    //  use string "noEcho" to just get a string return only
    $args = func_get_args();
    $doEcho = TRUE; $sb;
    if ($args) {
        $sb = '<div style="margin: 1em 0;"><fieldset style="display:inline-block;padding:0em 3em 1em 1em;"><legend><b>preDump: '.count($args).' Parameters Found.</b></legend>';
        foreach (func_get_args() as $arg) {
            if (gettype($arg) == 'string') if ($arg == 'noEcho') { $doEcho = FALSE; $sb = preg_replace('/(preDump: )[0-9]+/', 'preDump: '.(count($args)-1), $sb); continue; }
            $sb .= '<pre data-type="'.gettype($arg).'"';
            switch (gettype($arg)) {
                case "boolean":
                case "integer":
                    $sb .= ' data-dump="json_encode"><p style="border-bottom:1px solid;margin:0;padding:0 0 0 1em;"><b>gettype('.gettype($arg).')</b></p><p>';
                    $sb .= json_encode($arg);
                    break;
                case "string":
                    $sb .= ' data-dump="echo"><p style="border-bottom:1px solid;margin:0;padding:0 0 0 1em;"><b>gettype('.gettype($arg).')</b></p><p>';
                    $sb .= $arg;
                    break;
                default:
                    $sb .= ' data-dump="var_dump"';
                    if (is_object($arg)) $sb .= 'data-class="'.get_class($arg).'"';
                    $sb .= '><p style="border-bottom:1px solid;margin:0;padding:0 0 0 1em;"><b>gettype('.gettype($arg).')';
                    if (is_object($arg)) $sb .= ' ['.get_class($arg).']';
                    $sb .= '</b></p><p>';
                    ob_start();
                    var_dump($arg);
                    $sb .= ob_get_clean();
                    if (ob_get_length()) ob_end_clean();
            }
            $sb .= '</p></pre>';
        }
        $sb .= '</fieldset></div>';
    }
    else {
        $sb = '<div style="margin: 1em 0;"><fieldset style="display:inline-block;"><legend><b>preDump: [ERROR]</b></legend><h3>No Parameters Found</h3></fieldset></div>';
    }
    if ($doEcho) echo($sb);
    return $sb;
}

Use as simple as:

preDump($array1, $array2, $array3, $array4, $array5);
preDump('string', TRUE, 101, array( 'test', 'array' ), (object)array('object'=>'array'), $xmlVariable);

Even get return as string only (not printed to browser) by passing the string noEcho as a parameter.

$bob = preDump($array1, $array2, 'noEcho');

Upvotes: 0

John Conde
John Conde

Reputation: 219934

Just wrap your dumps in <pre> tags:

<pre><?php print_r($array); ?></pre>

Also, if you view the source of your page it will be all pretty like this, too. It's just because HTML ignores whitespace that you see it all on one line in your browser.

Upvotes: 5

Related Questions