/ Published in: ActionScript
Recursively trace an array or object in Actionscript.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// To call this function, use "print_r($array);". //It will return it and trace it. The Print_A //function is supporting and not to be called on is own. // Minimized private function print_a( obj, indent ) { if (indent == null) indent = "";var out = "";for ( var item in obj ) {if(substring(item,0,1)!="_"){var bracks = ['{','}'];var newindent=indent;var isNum = ( !isNaN(Number(item))) ? 'a':'';if(isNum=='a') bracks=['[',']'];if (typeof( obj[item] ) == "object" ){out+= indent+bracks[0] + item + bracks[1]+" => Object\n",newindent = indent + " " ;}else{out+= indent+isNum+bracks[0] + item + bracks[1]+" => " + obj[item]+"\n" ;}out+= print_a( obj[item], newindent+" " ) ;}}return out; } public function print_r( obj, indent ){ var res = this.print_a(obj,indent);var returner = "Printing " + obj._name + chr(13) + res;trace(returner);return returner; } // Regular private function print_a( obj, indent ) { if (indent == null) indent = ""; var out = ""; for ( var item in obj ) { if(substring(item,0,1)!="_"){ var bracks = ['{','}']; var newindent=indent; // for indenting obj children var isNum = ( !isNaN(Number(item))) ? 'a':''; // tells if obj is array or obj if(isNum=='a') bracks=['[',']']; //changes brackets for arrays if (typeof( obj[item] ) == "object" ) out+= indent+bracks[0] + item + bracks[1]+" => Object\n",newindent = indent + " " ; else out+= indent+isNum+bracks[0] + item + bracks[1]+" => " + obj[item]+"\n" ; out+= print_a( obj[item], newindent+" " ) ; } } return out; } public function print_r( obj, indent ){ var res = this.print_a(obj,indent); var returner = "Printing " + obj._name + chr(13) + res; trace(returner) return returner }