将数组的var_dump转换回数组变量 [英] Convert var_dump of array back to array variable
问题描述
直到今天我还没有真正考虑过这个问题,但是在网上搜索后,我什么都没找到.也许我不是在搜索中说出正确的字眼.
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')));
在var_dumped时:
When var_dumped:
array(2) { ["this"]=> array(1) { ["is"]=> string(3) "the" } ["challenge"]=> array(1) { ["for"]=> array(1) { [0]=> string(3) "you" } } }
挑战是这样的:将数组重新编译为适用于PHP的可用数组的最佳优化方法是什么?就像undump_var()
函数一样.在浏览器中输出的数据是否全部在一行上,还是包含换行符,作为输出到终端.
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.
更新:注意.我熟悉序列化和反序列化的人员.我不是在寻找替代解决方案.看看是否可以通过优化和创新的方式来完成代码是一个代码挑战.因此,此处 serialize和var_export不是解决方案.它们也不是最佳答案.
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.
推荐答案
serialize
是您要的内容. var_export
将呈现PHP可解析的数组语法,而serialize
将呈现非人类可读但可逆的数组到字符串"转换...
var_export
or serialize
is what you're looking for. var_export
will render a PHP parsable array syntax, and serialize
will render a non-human readable but reversible "array to string" conversion...
编辑,好吧,挑战:
基本上,我将输出转换为序列化的字符串(然后将其反序列化).我不认为这是完美的,但它似乎可以在我尝试过的一些非常复杂的结构上工作.
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...
function unvar_dump($str) {
if (strpos($str, "\n") === false) {
//Add new lines:
$regex = array(
'#(\\[.*?\\]=>)#',
'#(string\\(|int\\(|float\\(|array\\(|NULL|object\\(|})#',
);
$str = preg_replace($regex, "\n\\1", $str);
$str = trim($str);
}
$regex = array(
'#^\\040*NULL\\040*$#m',
'#^\\s*array\\((.*?)\\)\\s*{\\s*$#m',
'#^\\s*string\\((.*?)\\)\\s*(.*?)$#m',
'#^\\s*int\\((.*?)\\)\\s*$#m',
'#^\\s*bool\\(true\\)\\s*$#m',
'#^\\s*bool\\(false\\)\\s*$#m',
'#^\\s*float\\((.*?)\\)\\s*$#m',
'#^\\s*\[(\\d+)\\]\\s*=>\\s*$#m',
'#\\s*?\\r?\\n\\s*#m',
);
$replace = array(
'N',
'a:\\1:{',
's:\\1:\\2',
'i:\\1',
'b:1',
'b:0',
'd:\\1',
'i:\\1',
';'
);
$serialized = preg_replace($regex, $replace, $str);
$func = create_function(
'$match',
'return "s:".strlen($match[1]).":\\"".$match[1]."\\"";'
);
$serialized = preg_replace_callback(
'#\\s*\\["(.*?)"\\]\\s*=>#',
$func,
$serialized
);
$func = create_function(
'$match',
'return "O:".strlen($match[1]).":\\"".$match[1]."\\":".$match[2].":{";'
);
$serialized = preg_replace_callback(
'#object\\((.*?)\\).*?\\((\\d+)\\)\\s*{\\s*;#',
$func,
$serialized
);
$serialized = preg_replace(
array('#};#', '#{;#'),
array('}', '{'),
$serialized
);
return unserialize($serialized);
}
我在一个复杂的结构上对其进行了测试,例如:
I tested it on a complex structure such as:
array(4) {
["foo"]=>
string(8) "Foo"bar""
[0]=>
int(4)
[5]=>
float(43.2)
["af"]=>
array(3) {
[0]=>
string(3) "123"
[1]=>
object(stdClass)#2 (2) {
["bar"]=>
string(4) "bart"
["foo"]=>
array(1) {
[0]=>
string(2) "re"
}
}
[2]=>
NULL
}
}
这篇关于将数组的var_dump转换回数组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!