如何在PHP中使用循环和echo显示多维数组(2D、3D、4D和更多),如var_dump?
这是我的阵列:如何在PHP中使用循环和echo显示多维数组(2D、3D、4D和更多),如var_dump?,php,arrays,function,recursion,var-dump,Php,Arrays,Function,Recursion,Var Dump,这是我的阵列: $arr=array( array( array(array( "value1", "value2" ), "value2" ), array( "value3", "value4" ) ), array( array( "value5", "value6" ), array( "value7", "value8" ) ) ); 如果我们用var_dump打印它,它显示如下: array(2)
$arr=array(
array(
array(array( "value1", "value2" ), "value2" ),
array( "value3", "value4" )
),
array(
array( "value5", "value6" ),
array( "value7", "value8" )
)
);
如果我们用var_dump打印它,它显示如下:
array(2) {
[0]=>
array(2) {
[0]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(6) "value1"
[1]=>
string(6) "value2"
}
[1]=>
string(6) "value2"
}
[1]=>
array(2) {
[0]=>
string(6) "value3"
[1]=>
string(6) "value4"
}
}
[1]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(6) "value5"
[1]=>
string(6) "value6"
}
[1]=>
array(2) {
[0]=>
string(6) "value7"
[1]=>
string(6) "value8"
}
}
}
我想写一个函数,可以打印每个多维数组,只打印回显和循环,我该怎么做
这是我的代码:
function recursive($array)
{
foreach ($array as $key => $value) {
//If $value is an array.
if (is_array($value)) {
//We need to loop through it.
recursive($value);
} else {
if (gettype($value)!=="string") {
//It is not an array, so print it out.
echo $key . ": " .gettype($value).count($value). $value, '<br>';
} else {
echo "[".$key . "]=>" .gettype($value)."(".strlen($value).") \"". $value."\"", '<br>';
}
}
}
}
recursive($arr);
此代码不能像var_dump那样打印
我想通过我的函数像var_dump一样打印,我如何才能完成这项工作?函数var_debug($variable,$strlen=100,$width=25,$depth=10,$I=0,&$objects=array())
function var_debug($variable,$strlen=100,$width=25,$depth=10,$i=0,&$objects = array())
{
$search = array("\0", "\a", "\b", "\f", "\n", "\r", "\t", "\v");
$replace = array('\0', '\a', '\b', '\f', '\n', '\r', '\t', '\v');
$string = '';
switch(gettype($variable)) {
case 'boolean': $string.= $variable?'true':'false'; break;
case 'integer': $string.= $variable; break;
case 'double': $string.= $variable; break;
case 'resource': $string.= '[resource]'; break;
case 'NULL': $string.= "null"; break;
case 'unknown type': $string.= '???'; break;
case 'string':
$len = strlen($variable);
$variable = str_replace($search,$replace,substr($variable,0,$strlen),$count);
$variable = substr($variable,0,$strlen);
if ($len<$strlen) $string.= '"'.$variable.'"';
else $string.= 'string('.$len.'): "'.$variable.'"...';
break;
case 'array':
$len = count($variable);
if ($i==$depth) $string.= 'array('.$len.') {...}';
elseif(!$len) $string.= 'array(0) {}';
else {
$keys = array_keys($variable);
$spaces = str_repeat(' ',$i*2);
$string.= "array($len)\n".$spaces.'{';
$count=0;
foreach($keys as $key) {
if ($count==$width) {
$string.= "\n".$spaces." ...";
break;
}
$string.= "\n".$spaces." [$key] => ";
$string.= var_debug($variable[$key],$strlen,$width,$depth,$i+1,$objects);
$count++;
}
$string.="\n".$spaces.'}';
}
break;
case 'object':
$id = array_search($variable,$objects,true);
if ($id!==false)
$string.=get_class($variable).'#'.($id+1).' {...}';
else if($i==$depth)
$string.=get_class($variable).' {...}';
else {
$id = array_push($objects,$variable);
$array = (array)$variable;
$spaces = str_repeat(' ',$i*2);
$string.= get_class($variable)."#$id\n".$spaces.'{';
$properties = array_keys($array);
foreach($properties as $property) {
$name = str_replace("\0",':',trim($property));
$string.= "\n".$spaces." [$name] => ";
$string.= var_debug($array[$property],$strlen,$width,$depth,$i+1,$objects);
}
$string.= "\n".$spaces.'}';
}
break;
}
if ($i>0) return $string;
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
do $caller = array_shift($backtrace); while ($caller && !isset($caller['file']));
if ($caller) $string = $caller['file'].':'.$caller['line']."\n".$string;
echo $string;
}
{
$search=array(“\0”、“\a”、“\b”、“\f”、“\n”、“\r”、“\t”、“\v”);
$replace=array('\0','\a','\b','\f','\n','\r','\t','\v');
$string='';
开关(gettype($variable)){
大小写'boolean':$string.=$variable'true':'false';break;
大小写“整数”:$string.=$variable;break;
大小写'double':$string.=$variable;break;
案例'resource':$string.='[resource]';中断;
大小写'NULL':$string.=“NULL”;中断;
大小写“未知类型”:$string.=”?”;中断;
大小写“string”:
$len=strlen($variable);
$variable=str_replace($search,$replace,substr($variable,0,$strlen),$count);
$variable=substr($variable,0,$strlen);
if($len0)返回$string;
$backtrace=debug\u backtrace(debug\u backtrace\u IGNORE\u ARGS);
do$caller=array_shift($backtrace);while($caller&&!isset($caller['file']);
如果($caller)$string=$caller['file'].:'.$caller['line'.]。“\n”。$string;
echo$字符串;
}
信用证:如果我不想使用
$spaces
我可以删除$i=0
,但是如果我删除如果($i>0)返回$string;
数组输出会像string!为什么?
function var_debug($variable,$strlen=100,$width=25,$depth=10,$i=0,&$objects = array())
{
$search = array("\0", "\a", "\b", "\f", "\n", "\r", "\t", "\v");
$replace = array('\0', '\a', '\b', '\f', '\n', '\r', '\t', '\v');
$string = '';
switch(gettype($variable)) {
case 'boolean': $string.= $variable?'true':'false'; break;
case 'integer': $string.= $variable; break;
case 'double': $string.= $variable; break;
case 'resource': $string.= '[resource]'; break;
case 'NULL': $string.= "null"; break;
case 'unknown type': $string.= '???'; break;
case 'string':
$len = strlen($variable);
$variable = str_replace($search,$replace,substr($variable,0,$strlen),$count);
$variable = substr($variable,0,$strlen);
if ($len<$strlen) $string.= '"'.$variable.'"';
else $string.= 'string('.$len.'): "'.$variable.'"...';
break;
case 'array':
$len = count($variable);
if ($i==$depth) $string.= 'array('.$len.') {...}';
elseif(!$len) $string.= 'array(0) {}';
else {
$keys = array_keys($variable);
$spaces = str_repeat(' ',$i*2);
$string.= "array($len)\n".$spaces.'{';
$count=0;
foreach($keys as $key) {
if ($count==$width) {
$string.= "\n".$spaces." ...";
break;
}
$string.= "\n".$spaces." [$key] => ";
$string.= var_debug($variable[$key],$strlen,$width,$depth,$i+1,$objects);
$count++;
}
$string.="\n".$spaces.'}';
}
break;
case 'object':
$id = array_search($variable,$objects,true);
if ($id!==false)
$string.=get_class($variable).'#'.($id+1).' {...}';
else if($i==$depth)
$string.=get_class($variable).' {...}';
else {
$id = array_push($objects,$variable);
$array = (array)$variable;
$spaces = str_repeat(' ',$i*2);
$string.= get_class($variable)."#$id\n".$spaces.'{';
$properties = array_keys($array);
foreach($properties as $property) {
$name = str_replace("\0",':',trim($property));
$string.= "\n".$spaces." [$name] => ";
$string.= var_debug($array[$property],$strlen,$width,$depth,$i+1,$objects);
}
$string.= "\n".$spaces.'}';
}
break;
}
if ($i>0) return $string;
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
do $caller = array_shift($backtrace); while ($caller && !isset($caller['file']));
if ($caller) $string = $caller['file'].':'.$caller['line']."\n".$string;
echo $string;
}