{"id":182,"date":"2011-02-07T14:47:36","date_gmt":"2011-02-07T13:47:36","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=182"},"modified":"2011-02-07T14:48:08","modified_gmt":"2011-02-07T13:48:08","slug":"phps-print_r-for-as3","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/phps-print_r-for-as3\/","title":{"rendered":"Php&#8217;s print_r for as3"},"content":{"rendered":"<p>Here is really useful class tools for as3 based on php function found on <a href=\"http:\/\/dev.base86.com\"><strong>http:\/\/dev.base86.com<\/strong><\/a> website.<\/p>\n<p>Exemple use :<\/p>\n<pre lang=\"actionscript3\">import com.base86.Tools;\r\n\r\nvar obj:Array = new Array();\r\nobj['a'] = { one: 1, two: 2, three: 3 };\r\nobj['b'] = [ 'one', 'two', [ 'three' ] ];\r\nobj['c'] = new MovieClip();\r\n\r\nTools.pr(obj);\r\n<\/pre>\n<p>The console will display:<\/p>\n<pre lang=\"actionscript3\">(array) {\r\n\t[a] => (object) {\r\n\t\t[two] => (number) 2\r\n\t\t[three] => (number) 3\r\n\t\t[one] => (number) 1\r\n\t}\r\n\t[b] => (array) {\r\n\t\t[0] => (string) one\r\n\t\t[1] => (string) two\r\n\t\t[2] => (array) {\r\n\t\t\t[0] => (string) three\r\n\t\t}\r\n\t}\r\n\t => (object) [object MovieClip]\r\n}\r\n<\/pre>\n<p>And the class now :<\/p>\n<pre lang=\"actionscript3\" line=\"1\">package com.base86 {\r\n\tpublic class Tools {\r\n\t\t\/**\r\n\t\t * An equivalent of PHP's recursive print function print_r, which displays objects and arrays in a way that's readable by humans\r\n\t\t * @param obj    Object to be printed\r\n\t\t * @param level  (Optional) Current recursivity level, used for recursive calls\r\n\t\t * @param output (Optional) The output, used for recursive calls\r\n\t\t *\/\r\n\t\tpublic static function pr(obj:*, level:int = 0, output:String = ''):* {\r\n\t\t\tif(level == 0) output = '('+ Tools.typeOf(obj) +') {\\n';\r\n\t\t\telse if(level == 10) return output;\r\n\t\t\t\r\n\t\t\tvar tabs:String = '\\t';\r\n\t\t\tfor(var i:int = 0; i < level; i++, tabs += '\\t') { }\r\n\t\t\tfor(var child:* in obj) {\r\n\t\t\t\toutput += tabs +'['+ child +'] => ('+  Tools.typeOf(obj[child]) +') ';\r\n\t\t\t\t\r\n\t\t\t\tif(Tools.count(obj[child]) == 0) output += obj[child];\r\n\t\t\t\t\r\n\t\t\t\tvar childOutput:String = '';\r\n\t\t\t\tif(typeof obj[child] != 'xml') {\r\n\t\t\t\t\tchildOutput = Tools.pr(obj[child], level + 1);\r\n\t\t\t\t}\r\n\t\t\t\tif(childOutput != '') {\r\n\t\t\t\t\toutput += '{\\n'+ childOutput + tabs +'}';\r\n\t\t\t\t}\r\n\t\t\t\toutput += '\\n';\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif(level == 0) trace(output +'}\\n');\r\n\t\t\telse return output;\r\n\t\t}\r\n\t\t\r\n\t\t\/**\r\n\t\t * An extended version of the 'typeof' function\r\n\t\t * @param \tvariable\r\n\t\t * @return\tReturns the type of the variable\r\n\t\t *\/\r\n\t\tpublic static function typeOf(variable:*):String {\r\n\t\t\tif(variable is Array) return 'array';\r\n\t\t\telse if(variable is Date) return 'date';\r\n\t\t\telse return typeof variable;\r\n\t\t}\r\n\t\t\r\n\t\t\/**\r\n\t\t * Returns the size of an object\r\n\t\t * @param obj Object to be counted\r\n\t\t *\/\r\n\t\tpublic static function count(obj:Object):uint {\r\n\t\t\tif(Tools.typeOf(obj) == 'array') return obj.length;\r\n\t\t\telse {\r\n\t\t\t\tvar len:uint = 0;\r\n\t\t\t\tfor(var item:* in obj) {\r\n\t\t\t\t\tif(item != 'mx_internal_uid') len++;\r\n\t\t\t\t}\r\n\t\t\t\treturn len;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is really useful class tools for as3 based on php function found on http:\/\/dev.base86.com website. Exemple use : import com.base86.Tools; var obj:Array = new Array(); obj[&#8216;a&#8217;] = { one: 1, two: 2, three: 3 }; obj[&#8216;b&#8217;] = [ &#8216;one&#8217;, &#8216;two&#8217;, [ &#8216;three&#8217; ] ]; obj[&#8216;c&#8217;] = new MovieClip(); Tools.pr(obj); The console will display: (array) &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/phps-print_r-for-as3\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Php&#8217;s print_r for as3<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4],"tags":[18,15,19],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/182"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":2,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":184,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/182\/revisions\/184"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}