php var dump 美化,PHP中一个更漂亮/信息丰富的Var_dump替代方案?

这段代码演示了如何使用PHP遍历并打印一个包含嵌套对象和数组的数据结构。它定义了一个名为`dump`的函数,该函数递归地处理数值、字符串、NULL、布尔值、数组和对象,展示了一个清晰的层次结构。示例中,`$options`变量包含了`Orchestra`、`Car`、`Bus`和`TV`等元素,每个元素可能是字符串、数组或对象,展示了PHP处理复杂数据结构的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Anthony Scai..

7

我用的一个完整的例子......

 
 

//*********** Set up some sample data

$obj = new stdClass;

$obj->a=123;

$obj->pl=44;

$obj->l=array(31,32);

$options = array(

'Orchestra'=>array(1=>'Strings', 8=>'Brass', 9=>$obj, 3=>'Woodwind', 16=>'Percussion'),

2=>'Car',

4=>'Bus',

'TV'=>array(21=>'Only Fools', 215=>'Brass Eye', 23=>'Vic Bob',44=>null, 89=>false));

//*********** Define the function

function dump($data, $indent=0) {

$retval = '';

$prefix=\str_repeat(' | ', $indent);

if (\is_numeric($data)) $retval.= "Number: $data";

elseif (\is_string($data)) $retval.= "String: '$data'";

elseif (\is_null($data)) $retval.= "NULL";

elseif ($data===true) $retval.= "TRUE";

elseif ($data===false) $retval.= "FALSE";

elseif (is_array($data)) {

$retval.= "Array (".count($data).')';

$indent++;

foreach($data AS $key => $value) {

$retval.= "\n$prefix [$key] = ";

$retval.= dump($value, $indent);

}

}

elseif (is_object($data)) {

$retval.= "Object (".get_class($data).")";

$indent++;

foreach($data AS $key => $value) {

$retval.= "\n$prefix $key -> ";

$retval.= dump($value, $indent);

}

}

return $retval;

}

//*********** Dump the data

echo dump($options);

?>

输出......

Array (4)

[Orchestra] = Array (5)

| [1] = String: 'Strings'

| [8] = String: 'Brass'

| [9] = Object (stdClass)

| | a -> Number: 123

| | pl -> Number: 44

| | l -> Array (2)

| | | [0] = Number: 31

| | | [1] = Number: 32

| [3] = String: 'Woodwind'

| [16] = String: 'Percussion'

[2] = String: 'Car'

[4] = String: 'Bus'

[TV] = Array (5)

| [21] = String: 'Only Fools'

| [215] = String: 'Brass Eye'

| [23] = String: 'Vic Bob'

| [44] = NULL

| [89] = FALSE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值