将数组的 var_dump 转换回数组变量
直到今天我才真正考虑过这个问题,但在网上搜索后我并没有真正找到任何东西。也许我在搜索中的措辞不正确。
给定一个数组(多维或非多维):
$data = array('this' => array('is' => 'the'), 'challenge' => array('for' => array('you')));
当 var_dumped 时:
array(2) { ["this"]=> array(1) { ["is"]=> string(3) "the" } ["challenge"]=> array(1) { ["for"]=> array(1) { [0]=> string(3) "you" } } }
挑战是这样的:将数组重新编译为 PHP 可用数组的最佳优化方法是什么?就像 undump_var()
函数一样。数据是否全部在一行上作为浏览器中的输出,或者是否包含换行符作为终端的输出。
这只是正则表达式的问题吗?或者还有其他办法吗?我正在寻找创造力。
更新:注意。我熟悉序列化和反序列化的人。我并不是在寻找替代解决方案。这是一个代码挑战,看看是否可以以优化和创造性的方式完成。所以这里serialize 和var_export 不是解决方案。它们也不是最好的答案。
I have never really thought about this until today, but after searching the web I didn't really find anything. Maybe I wasn't wording it right in the search.
Given an array (of multiple dimensions or not):
$data = array('this' => array('is' => 'the'), 'challenge' => array('for' => array('you')));
When var_dumped:
array(2) { ["this"]=> array(1) { ["is"]=> string(3) "the" } ["challenge"]=> array(1) { ["for"]=> array(1) { [0]=> string(3) "you" } } }
The challenge is this: What is the best optimized method for recompiling the array to a useable array for PHP? Like an undump_var()
function. Whether the data is all on one line as output in a browser or whether it contains the line breaks as output to terminal.
Is it just a matter of regex? Or is there some other way? I am looking for creativity.
UPDATE: Note. I am familiar with serialize and unserialize folks. I am not looking for alternative solutions. This is a code challenge to see if it can be done in an optimized and creative way. So serialize and var_export are not solutions here. Nor are they the best answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
var_export
或serialize
就是您要寻找的。var_export
将呈现 PHP 可解析数组语法,serialize
将呈现非人类可读但可逆的“数组到字符串”转换...编辑 好吧,迎接挑战:
基本上,我将输出转换为序列化字符串(然后将其反序列化)。我并不认为这是完美的,但它似乎适用于我尝试过的一些相当复杂的结构......
我在复杂的结构上测试了它,例如:
var_export
orserialize
is what you're looking for.var_export
will render a PHP parsable array syntax, andserialize
will render a non-human readable but reversible "array to string" conversion...Edit Alright, for the challenge:
Basically, I convert the output into a serialized string (and then unserialize it). I don't claim this to be perfect, but it appears to work on some pretty complex structures that I've tried...
I tested it on a complex structure such as:
除了根据类型手动解析之外没有其他方法。
我没有添加对对象的支持,但它与数组非常相似;您只需要执行一些反射魔法即可填充公共属性并且不触发构造函数。
编辑:添加了对对象的支持...反射魔术...
(在增加字符串位置计数器
$i
时,这里有很多“魔术”数字,大部分只是关键字的字符串长度和一些括号等.)There's no other way than manual parsing depending on the type.
I didn't add support for objects, but it's very similar to the arrays one; you just need to do some reflection magic to populate not only public properties and to not trigger the constructor.
EDIT: Added support for objects... Reflection magic...
(Here are a lot of "magic" numbers when incrementing string position counter
$i
, mostly just string lengths of the keywords and some parenthesis etc.)如果你想对这样的数组进行编码/解码,你应该使用
var_export()
,它在 PHP 数组中生成输出,例如:可能是它的结果。不过,您必须使用
eval()
来取回数组,这是一种潜在危险的方式(特别是因为eval()
真正执行 PHP 代码,因此简单的代码注入可以使黑客能够控制您的 PHP 脚本)。一些更好的解决方案是
serialize()
,它创建任何数组或的序列化版本目的;和json_encode()
,它使用 JSON 格式(更适合不同语言之间的数据交换)。If you want to encode/decode an array like this, you should either use
var_export()
, which generates output in PHP's array for, for instance:could be the result of it. You would have to use
eval()
to get the array back, though, and that is a potentially dangerous way (especially sinceeval()
really executes PHP code, so a simple code injection could make hackers able to gain control over your PHP script).Some even better solutions are
serialize()
, which creates a serialized version of any array or object; andjson_encode()
, which encodes any array or object with the JSON format (which is more preferred for data exchange between different languages).诀窍是通过代码块和“字符串”进行匹配,并且在字符串上不执行任何操作,而是进行替换:
输出:(
删除从 0 开始的升序数字键需要一些额外的计算,这可以在
repl
函数中完成。)ps. 这并不能解决包含
"
的字符串问题,但 var_dump 似乎并没有解决不能转义字符串内容,没有办法可靠地解决这个问题(您可以匹配\["[^"]*"\]
但字符串可能包含"] 以及)
The trick is to match by chunks of code and
"strings"
, and on strings do nothing but otherwise do the replacements:outputs:
(removing ascending numeric keys starting at 0 takes a little extra accounting, which can be done in the
repl
function.)ps. this doesn't solve the problem of strings containing
"
, but as it seems that var_dump doesn't escape string contents, there is no way to solve that reliably. (you could match\["[^"]*"\]
but a string may contain"]
as well)使用正则表达式将 array(.) { (.*) } 更改为 array($1) 并评估代码,这并不像编写的那么容易,因为您必须处理匹配的括号等,只是如何找到解决方案的线索;)
Use regexp to change array(.) { (.*) } to array($1) and eval the code, this is not so easy as written because You have to deal with matching brackets etc., just a clue on how to find solution ;)
更新为不使用 create_function,因为它从 PHP 7.2.0 开始已弃用。相反,它被替换为使用匿名函数:
Updated to NOT USE create_function, as it is DEPRECATED as of PHP 7.2.0. Instead it is replaced to use anonymous functions:
我认为您正在寻找
serialize
函数:它允许您以可读格式保存数组的内容,稍后您可以使用
反序列化
函数。使用这些函数,您甚至可以在文本/平面文件以及数据库中存储/检索数组。
I think you are looking for the
serialize
function:It allows you to save the contents of array in readable format and later you can read the array back with
unserialize
function.Using these functions, you can store/retrieve the arrays even in text/flat files as well as database.