类似于var_dump() 区别在于它可以把输出当成字符串返回,而不是直接打印
function array_dump( $var ) {
$resString = "" ;
// The gettype function returns the data type
// of its parameter.
switch ( gettype ( $var )) {
// inter, double, and strings are simply
// displayed.
case ' integer ' :
case ' double ' :
case ' string ' :
$resString .= " $var\n " ;
break ;
// array datatypes need to specially
// handled.
case ' array ' :
$resString .= " Array\n(\n " ;
// if the array has no entries, display
// a message.
if ( ! Count ( $var )) {
$resString .= " \tEmpty Array.\n " ;
}
else {
// use a do loop to iterate over the
// entries in the array.
do {
$resString .= " \t[ " . key ( $var ) . " ] => " ;
// perform the magic of recursion using the
// VALUE of the current key/value pair.
$resString .= array_dump( $var [ key ( $var )]);
} while ( next ( $var ));
// end the HTML table after all of the
//array entries have been displayed.
}
$resString .= " )\n " ;
break ;
// switch statements should always have a default
// clause - just in case the data has a value
// your program doesn't expect.
default :
$resString .= " unknown data type\n " ;
break ;
}
return $resString ;
}