javascript 实现类似php中的print_r函数

javascript 实现类似php中的print_r函数

源代码

function print_r(arr){
	var output;
	if(arr.constructor == Array || arr.constructor == Object){
		output="Array\n";
		output=output+"(\n";
		for(var p in arr){
			if(arr[p].constructor == Array || arr[p].constructor == Object){
				output=output+"    ["+p+"] => "+print_r(arr[p]);
			} else {
				output=output+"    ["+p+"] => "+arr[p]+"\n";
			}
		}
		output=output+")\n";
	}
	return output;
}

用法:

var _arr={"a":"1","b":"2","c":"3","d":"4","e":[1,2,3]}
document.write(print_r(_arr));

返回格式:
Array
(
[a] => 1
[b] => 2
[c] => 3
[d] => 4
[e] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)

发表评论

电子邮件地址不会被公开。 必填项已用*标注