Actionscript 3 equivalent of PHP's print_r

Actionscript 3 equivalent of PHP's print_r – print_r, flash, flex, as3, recursive, actionscript 3

Here's a handy little function for easing the development process with Actionscript 3 in Flex and Flash: an equivalent of PHP's recursive print function print_r, which displays objects and arrays in a way that's readable by humans.

Code for Flex:

import mx.controls.Alert;
 
public function pr(obj:*, level:int = 0, output:String = ""):* {
    var tabs:String = "";
    for(var i:int = 0; i < level; i++, tabs += "\t");
   
    for(var child:* in obj){
            output += tabs +"["+ child +"] => "+ obj[child];
           
            var childOutput:String = pr(obj[child], level+1);
            if(childOutput != '') output += ' {\n'+ childOutput + tabs +'}';
           
            output += "\n";
    }
   
    if(level == 0) Alert.show(output);
    else return output;
}

public function test():void {
    var obj:Object = new Object();
    obj.var1 = "test";
    obj.var2 = { var2a: "a", var2b: 10 }
    pr(obj);
    pr([ "a", "b", "c" ]);
}

Code for Flash:

function pr(obj:*, level:int = 0, output:String = ""):* {
    var tabs:String = "";
    for(var i:int = 0; i < level; i++, tabs += "\t");
    
    for(var child:* in obj) {
        output += tabs +"["+ child +"] => "+ obj[child];
        
        var childOutput:String = pr(obj[child], level+1);
        if(childOutput != '') output += ' {\n'+ childOutput + tabs +'}';
        
        output += "\n";
    }
    
    if(level == 0) trace(output);
    else return output;
 }
  
 //Test
 var obj:Object = new Object();
 obj.var1 = "test";
 obj.var2 = { var2a: "a", var2b: 10 }
  
 pr(obj);
 pr([ "a", "b", "c" ]);

Both tests will display:

[var1] => test
[var2] => [object Object] {
    [var2b] => 10
    [var2a] => a
 }
 
[0] => a
[1] => b
[2] => c
Author rcz Time Oct 14, 2008 Comment on 'Actionscript 3 equivalent of PHP's print_r' Add comment Bookmark 'Actionscript 3 equivalent of PHP's print_r' Bookmark Permalink to 'Actionscript 3 equivalent of PHP's print_r' Permalink
Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Netvouz Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Facebook Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Furl Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Technorati Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Stumble Upon Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Digg Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Del.icio.us

Comments (7)

Comment author j-n Time Feb 13, 2009 07:13 GMT #1
Thanks, it helped out a lot Face: Grin
Comment author Agustin Amenabar Comment author email aamenabar<at>yahoo.com Time Mar 19, 2009 05:46 GMT #2
DUDE!!!

I had rolled my own, but it wasn't as good as php's... this is even better... thank you so much...
How come nobody has commented here?
this is excelent!
thanks
Comment author dCa! Comment author email diegoavola<at>gmail.com Time Mar 24, 2009 11:44 GMT #3
Hi!
Thanks a lot for this code, but i always get this error in flash CS4, AS3:

"Error #1023: Stack overflow occurred."

Do you know why?
Thanks in advance!
Comment author Alex King Comment author website www.alexanderjamesking.com Time Apr 30, 2009 12:29 GMT #4
Excellent. I've been searching for something to dump objects to the console for ages.

Thanks!
Comment author nate Time Sep 7, 2009 06:17 GMT #5
@dCa! - You're passing in an object that is so deep that the recursion is hitting Flash's limit. You'll either need to call it on a smaller object (what are you passing in?) or modify the function to stop at a certain level. You could try adding:
if (level > 20) return '';

right at the beginning of the function, and then play around with that number until you stop getting the error.
Comment author nate Time Sep 7, 2009 06:18 GMT #6
ps. It would probably be a good idea to have that kind of limit in the function regardless, since it's pretty easy to find objects with looping references in Flash, which would lead to infinite recursion.
Comment author dexter Time Feb 1, 2010 11:13 GMT #7
Totally Excellent.
Its awesome to see handy features from other languages adapted to flash.

Now I wish there was something we could do to stop exceptions from being thrown whenever we try to access non-existent properties.

Add comment

Message
face:happy face:glasses face:devilgrin face:crying face:grin face:laughing face:sad face:kiss face:shocked face:wink face:monkey face:angel
Post comment
Your name
Your email or website (optional)
Icon
Confirmation code
Confirmation code