解析 var_export 输出文本以填充关联数组
不久前,我通过一些 API 获取了大量数据,并将其保存到一个平面文件中,执行简单的 var_dump 或 print_r。现在我正在处理数据,每行看起来像:
" 'middle_initial' => '', 'sid' => '1419843', '固定' => 'Y', '购物车重量' => '0', '键' => 'ABCD', '状态' => 'XX', '姓氏' => 'MNOP', '电子邮件' => '[电子邮件受保护]', 'city' => '伦敦', '街道地址' => '样本', '名字' => '斯帕什',”
现在我需要将这些数据恢复为数组格式。有什么办法可以做到这一点吗?
Sometime back I was getting alot of data via some API and I saved it into a flat file doing a simple var_dump or print_r. Now I am looking to process the data and each line looks like:
" 'middle_initial' => '', 'sid' => '1419843', 'fixed' => 'Y',
'cart_weight' => '0', 'key' => 'ABCD', 'state' => 'XX', 'last_name'
=> 'MNOP', 'email' => '[email protected]', 'city' => 'London',
'street_address' => 'Sample', 'first_name' => 'Sparsh',"
Now I need to get this data back into an array format. Is there a way I can do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先使用
explode()
函数爆炸字符串怎么样? >', ' 作为分隔符:这将使您得到一个如下所示的数组:
然后,迭代该列表,匹配
=>
每一侧的每个项目,并使用=>
的第一侧作为结果的键数据,第二个作为值:这会让你:
但是,说真的,您不应该以如此糟糕的格式存储数据:
print_r()
是为了显示数据而用于调试目的 - 而不是存储它以便稍后重新加载!如果要将数据存储到文本文件,请使用
serialize()
或json_encode()
,都可以使用unserialize()
或json_decode()
分别。What about first exploding the string with the
explode()
function, using', '
as a separator :Which would get you an array looking like this :
And, then, iterate over that list, matching for each item each side of the
=>
, and using the first side of=>
as the key of your resulting data, and the second as the value :Which would get you :
But, seriously, you should not store data in such an awful format :
print_r()
is made to display data, for debugging purposes -- not to store it an re-load it later !If you want to store data to a text file, use
serialize()
orjson_encode()
, which can both be restored usingunserialize()
orjson_decode()
, respectively.尽管我完全同意帕斯卡·马丁的观点,但如果您有此类数据需要处理,则以下内容(如帕斯卡的第一个建议提到的)可以根据您的实际内容起作用。不过,请帮自己一个忙,以可以可靠地放回 PHP 数组的格式存储数据(序列化、JSON、CSV 等)。
http://jfcoder.com/test/substr.php
注意,您需要调整如果数组成员的内容中有逗号(例如,使用 Pascal 关于
', '
标记的建议),请使用该函数。Although I wholeheartedly agree with Pascal Martin, if you have this kind of data to deal with, the following (as Pascal's first suggestion mentions) could work depending on your actual content. However, do yourself a favor and store your data in a format that can be reliably put back into a PHP array (serialize, JSON, CSV, etc...).
http://jfcoder.com/test/substr.php
Note, you would need to adjust the function if you have comma's within your array member's content (for instance, using Pascal's suggestion about the
', '
token).使用 Serialize 和 反序列化而不是var_dump。
在这种形式中,也许您可以尝试 a
,然后遍历数组并将这些对放在一个新数组中。
It would be better and much easier to work with Serialize and Unserialize instead of var_dump.
in that form, maybe you could try a
and then go through the array and put the pairs together in a new array.
只要是
print_r
的输出和一个数组,就可以使用我的小print_r转换器,PHP源代码可通过链接获取。分词器基于正则表达式,因此您可以根据需要采用它。解析器负责形成 PHP 数组值。
您将在我的个人资料页面上找到最新版本的链接。
As long as it's the output of
print_r
and an array, you can use my little print_r converter, PHP source code is available with the link.The tokenizer is based on regular expressions so you can adopt it to your needs. The parser deals with forming the PHP array value.
You will find the latest version linked on my profile page.