PHP的替代var_dump,可限制嵌套数组的深度 [英] Alternative var_dump for PHP that allows limiting depth of nested arrays
问题描述
我尝试在phpsh的命令行上使用var_dump,以获取有关某些变量的调试信息.但是该变量包含一个非常嵌套的数据结构.因此,使用默认的var_dump输出太多信息.
I try to use var_dump on command line with phpsh in order to get debugging information about some variable. But the variable contains a very deeply nested data structure. Therefore, using the default var_dump outputs too much information.
我想限制var_dump输出的深度级别.我发现XDebug的var_dump实现允许深度限制,如下所述: http://www.giorgiosironi.com/2009/07/how-to-stop-getting-megabytes-of-text.html
I want to limit the depth level of var_dump output. I found that XDebug's var_dump implementation allows depth limiting as described here: http://www.giorgiosironi.com/2009/07/how-to-stop-getting-megabytes-of-text.html
不幸的是,我也无法完成这项工作.我还不知道原因.我正在寻找是否有任何替代的var_dump实现可以尝试.
Unfortunately, I couldn't make this work neither. I don't know yet the reason for this. I am looking for if there are any alternative var_dump implementations to try.
推荐答案
检查此内容:
function print_array($array,$depth=1,$indentation=0){
if (is_array($array)){
echo "Array(\n";
foreach ($array as $key=>$value){
if(is_array($value)){
if($depth <= 0){
echo "max depth reached.";
}
else{
for($i=0;$i<$indentation;$i++){
echo " ";
}
echo $key."=Array(";
print_array($value,$depth-1,$indentation+1);
for($i=0;$i<$indentation;$i++){
echo " ";
}
echo ");";
}
}
else{
for($i=0;$i<$indentation;$i++){
echo " ";
}
echo $key."=>".$value."\n";
}
}
echo ");\n";
}
else{
echo "It is not an array\n";
}
}
这篇关于PHP的替代var_dump,可限制嵌套数组的深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!