Avicinnian
Avicinnian

Reputation: 1830

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 ;)!!

Upvotes: 2

Views: 135

Answers (7)

Lepidosteus
Lepidosteus

Reputation: 12047

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)

Upvotes: 6

Alex Howansky
Alex Howansky

Reputation: 53646

Xdebug can replace the plain text output of var_dump() with nicely formatted/colorized HTML. See http://xdebug.org/docs/display

Upvotes: 0

Kzqai
Kzqai

Reputation: 23102

Personally, I use a custom debug() function and <pre>:

function debug($val) {
    if (defined('DEBUG') && DEBUG) { // So debugging won't show on live.
        // Turn off debugs via debug=0 in url.
        if(!@$_REQUEST['debug'] && @$_REQUEST['debug'] !== 0){ // Can see a clean page by again by just appending a url parameter.
            $vals = func_get_args();
            foreach($vals as $val){
                    echo "<pre class='debug' style='font-weight:bold;font-size:1.1em;background-color:white;color:black;position:relative;z-index:10;clear:both;max-width:980px;overflow:auto'>";
                var_dump($val);
                echo "</pre>";
            }
        }
    }
}

You might also want to check into http://xdebug.org/ Xdebug, which formats var_dumps for html viewing. I've used that in the past as well.

Upvotes: 1

Matt Williamson
Matt Williamson

Reputation: 40243

Also, FirePHP can be installed and will send debug info to firebug in your browser like so: FB::log('Log message');

Upvotes: 0

Matt Williamson
Matt Williamson

Reputation: 40243

echo '<pre>'.print_r($myValue, true).'</pre>';

Upvotes: 0

Alex
Alex

Reputation: 9481

The trouble is that PHP formats the array using newline characters, not 'br' elements. And because the default Content-type is text/html, the browser ignores newline characters, you don't see them.

There are two solutions:

  1. View the page source
  2. Set the content type to text/plain:

    header('Content-type: text/plain');
    var_dump($some_array);
    

Upvotes: 0

hytest
hytest

Reputation: 1112

"view source" in your browser or

echo '<pre>'; var_dump($var); echo '</pre>';

Upvotes: 0

Related Questions