9

Actionscript 3 equivalent of PHP's print_r

Oct 14, 2008

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.

Update March 22, 2010: An update of this function has been released.

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
Add comment ↓

Comments (9)

j-n
Feb 13, 2009
Thanks, it helped out a lot Face: Grin
Agustin Amenabar
Mar 19, 2009
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
dCa!
Mar 24, 2009
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!
Alex King
Apr 30, 2009
Excellent. I've been searching for something to dump objects to the console for ages.

Thanks!
nate
Sep 7, 2009
@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.
nate
Sep 7, 2009
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.
dexter
Feb 1, 2010
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.
Dr Markus
Aug 2, 2010
Thanks for sharing this perfectly done script! I resume it will be awesome to help people in such manner to manage their programming doubts!
Joseph Merle
Sep 1, 2010
You could implement it with javascript. natural tinnitus cures

Add comment

Message
Name
E-mailaddress – optional, used for Gravatar
Confirmation code
Confirmation code