如何在PHP中从var_dump的输出创建数组? [英] How to create an array from output of var_dump in PHP?

查看:120
本文介绍了如何在PHP中从var_dump的输出创建数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在PHP中解析var_dump的输出以创建数组?

How can I parse the output of var_dump in PHP to create an array?

推荐答案

如果需要表示形式,请使用 var_export 也是有效的PHP代码

Use var_export if you want a representation which is also valid PHP code

$a = array (1, 2, array ("a", "b", "c"));
$dump=var_export($a, true);
echo $dump;

将显示

array (
 0 => 1,
 1 => 2,
 2 => 
 array (
   0 => 'a',
   1 => 'b',
   2 => 'c',
 ),
)

要将其转换为数组,您可以使用eval,例如

To turn that back into an array, you can use eval, e.g.

eval("\$foo=$dump;");
var_dump($foo);

不确定为什么,但是您想这样做.如果要在某个地方存储PHP数据结构,然后在以后重新创建它,请查看 serialize() unserialize(),它更适合此任务.

Not sure why you would want to do this though. If you want to store a PHP data structure somewhere and then recreate it later, check out serialize() and unserialize() which are more suited to this task.

这篇关于如何在PHP中从var_dump的输出创建数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆