views:

44

answers:

1

i want to write a function that prints multi-dimensional objects which are text (or integers, etc.) into <span></span> tags and arrays into unordered lists.

how do you make the function work recursively so that it prints everything regardless of what level it's at in the object?

thanks!

+1  A: 

Objects can be treated as arrays - try using foreach....

 function dump($obj, $prefix='')
 {
     foreach ($obj as $key=>$val) {
         print "$prefix attribute $key is a " . gettype($val) . "=";
         switch (gettype($val)) {
            case 'string':
            case 'boolean':
            case 'resource':
            case 'double':
            case 'NULL':
                var_export($val,true) . "\n";
                break;
            case 'object':
                print "(class=" . get_class($val) . ")";
            case 'array':
                print "(";
                dump($val, $prefix . '  ');
                print ")\n";
            default:
                print "????\n";
         }
     }
 }

C.

symcbean
...but you might want to restrict your recursion in case you've got circular references.
symcbean
perfect - thankyou!
significance