- 原生函数
php
$arr = [1,2,3,4];
$str = var_export($a,true);
var_dump($str);
- 自定义方法
php
class Export{
private static $space;
private static function do($a, string $prev){
$res = '';
$next = $prev . self::$space;
if (is_array($a)) {
$res .= '[';
foreach ($a as $k => $v) if (is_int($k)) {
$res .= $next . self::do($v, $next) . ',';
} else {
$res .= $next . self::do($k, $next) . ' => ' . self::do($v, $next) . ',';
}
return $res . $prev . ']';
} else if (is_scalar($a)) {
return is_string($a) ? '\'' . str_replace('\'', '\\\'', $a) . '\'' : json_encode($a);
} else return 'NULL';
}
public static function to($a, int $space_len = 2){
self::$space = str_repeat(' ', max($space_len, 2));
return self::do($a, PHP_EOL);
}
}
// $str = Export::to(转化的数据);
// var_dump($str);