PHP';s print r的Javascript等价物
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP';s print r的Javascript等价物相关的知识,希望对你有一定的参考价值。
Descriptionprint_r - Prints human-readable information about a variable
mixed print_r( mixed expression [, bool return] )
print_r() displays information about a variable in a way that's readable by humans.
Parameters
* expression
The expression to be printed.
* return
If you would like to capture the output of print_r(), use the return parameter. If this parameter is set to TRUE, print_r() will return its output, instead of printing it (which it does by default).
Return Values
If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects.
function print_r( array, return_val ) { // http://kevin.vanzonneveld.net // + original by: Michael White (http://crestidg.com) // + improved by: Ben Bryan // * example 1: print_r(1, true); // * returns 1: 1 var output = "", pad_char = " ", pad_val = 4; var formatArray = function (obj, cur_depth, pad_val, pad_char) { if (cur_depth > 0) { cur_depth++; } var base_pad = repeat_char(pad_val*cur_depth, pad_char); var thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char); var str = ""; if (obj instanceof Array || obj instanceof Object) { str += "Array " + base_pad + "( "; for (var key in obj) { if (obj[key] instanceof Array) { str += thick_pad + "["+key+"] => "+formatArray(obj[key], cur_depth+1, pad_val, pad_char); } else { str += thick_pad + "["+key+"] => " + obj[key] + " "; } } str += base_pad + ") "; } else if(obj == null || obj == undefined) { str = ''; } else { str = obj.toString(); } return str; }; var repeat_char = function (len, pad_char) { var str = ""; for(var i=0; i < len; i++) { str += pad_char; }; return str; }; output = formatArray(array, 0, pad_val, pad_char); if (return_val !== true) { document.write("<pre>" + output + "</pre>"); return true; } else { return output; } }
以上是关于PHP';s print r的Javascript等价物的主要内容,如果未能解决你的问题,请参考以下文章