0

I need to analyze an object in my code, but when I do a var_dump (or print_r) it just prints the object out with no structure, for example:

[0]=> object(simple_html_dom_node)#2185 (9) { ["nodetype"]=> int(1) ["tag"]=> string(3) "div" ["attr"]=> array(1) { ["class"]=> string(36) "element element--collection internal" } ["children"]=> array(2) { [0]=> object(simple_html_dom_node)#2187 (

I need to see it in a more structured format so I can see what is going on, i.e.:

object(simple_html_dom_node)#2185 (9) { 
    ["nodetype"]=> int(1) 
    ["tag"]=> string(3) "div" 
    ["attr"]=> array(1) 
    { 
        ["class"]=> string(36) "element element--collection internal" 
    } 
    ["children"]=> array(2) { 
        [0]=> object(simple_html_dom_node)#2187 (9) 

Does anyone know how to do this?

Brent Heigold
  • 1,213
  • 5
  • 24
  • 50

2 Answers2

1

The format you want is actually how var_dump() prints the object. The problem is that you're doing it in an HTML document, and the browser reformats it.

If you put it inside a <pre> tag, the browser will leave the formatting alone. So:

echo "<pre>"; var_dump($object); echo "</pre>";
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Try using var_export(), this function will give you a more readable structure of the object or data.

  • 1
    The browser will still reformat it so it won't be easy to read. The difference is that `var_export()` can be pasted into code to recreate the data. – Barmar Sep 24 '18 at 18:55
  • @Barmar using `var_export()` with the `
    ` can help him to solve the issue.
    –  Sep 24 '18 at 18:57
  • 1
    That's true. Although the output from `var_export()` is not actually as useful for debugging. It doesn't have the lengths of arrays and strings, for instance. – Barmar Sep 24 '18 at 18:59