如何在PHP中读取/写入结构化数据?
我想在 php 中读取、更新、插入来自/到平面文件的数据,结构如下(简单版本) 。处理它最简单的方法是什么?
$schools = array(
"PHCS"=> array(
"full_name"=> "Pacific Hills Christian School",
"version"=> "4.0.2b",
"etc"=> "etc"
),
"WAC"=> array(
"full_name"=> "Wollondilly Anglican College",
"version"=> "4.0.1",
"etc"=> "etc"
),
);
使用 YAML 时必须非常精确地使用语法和缩进,并且在 vi 中编辑 XML 也可能有点混乱。
所以我正在考虑使用 php 数组变量?如何将其保存为上述格式?
I want to read, update, insert data from/to a flat file in a structure like below (simple version) in php and also manually. What would be easiest way to deal with it?
$schools = array(
"PHCS"=> array(
"full_name"=> "Pacific Hills Christian School",
"version"=> "4.0.2b",
"etc"=> "etc"
),
"WAC"=> array(
"full_name"=> "Wollondilly Anglican College",
"version"=> "4.0.1",
"etc"=> "etc"
),
);
One has to be very precise with syntax and indents when using YAML and editing XML in vi could be bit messy too.
So I was thinking of using a php array variable? How can I save it in a format like above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想到了一些事情:
file_put_contents($file, serialize($schools))
编辑:啊,如果你需要通过文本编辑器进行编辑,那就不太好了。您可以改为使用 PHP 中的格式 -
然后您可以将其弹出到文件中并执行
$schools = require($schools)
,它将很好地导入。我将其用于数据固定装置,效果非常好。Couple of things come to mind:
file_put_contents($file, serialize($schools))
Edit: ah, if you need to edit via a text editor, those won't be very good. You could instead use the format you have in PHP -
Then you can pop that in a file and do
$schools = require($schools)
and it will import nicely. I use this for data fixtures, and it works quite well.好吧,如果您必须手动编辑文件,那么很难击败 YAML,因为该格式可能与您将要遇到的(至少在这个星球上)最接近“一种文件格式设计是为了便于手动编辑。”
您对“使用 PHP 数组变量”的思考在这里实际上是无关紧要的,因为这样的概念仅适用于程序的操作......在读取数据之后 > 并在随后将数据写回之前。它们与您决定使用的格式没有任何关系。
我建议您弄清楚什么数据表示对于人类来说最容易处理,然后围绕它编写计算机程序。此外,由于存在人类搞砸某些东西的可能性,因此程序应该不信任其输入。如果遇到垃圾,它应该“亲切地、有意义地、外交地”做出响应,例如:
Well, if you have to edit the file by hand, it's pretty hard to beat YAML because that format is probably about as close as you're going to come (on this planet, at least...) to "a file format that was designed to be friendly to being edited by hand."
You ponderings of "using a PHP array-variable" are really irrelevant here, because such notions only apply to the operation of a program ... after it has read the data in and before it subsequently writes the data back out. They don't have anything at all to do with what format you decided to use.
I'd suggest that you figure out what data representation is easiest for humans to deal with, then write the computer program around that. Furthermore, since the possibility exists that the humans snafu'd something, the program should be distrustful of its inputs. It should respond "graciously, meaningfully, and diplomatically" if it encounters garbage, for instance:
好吧,您可以尝试 jason_encode / jason_decode
它可以帮助您格式化所有数据以进行输出。
它还可以帮助您管理数据。
http://php.net/manual/en/function.json-encode.php
well, you may try jason_encode / jason_decode
it helps you format all your data for output.
It also helps u managing data.
http://php.net/manual/en/function.json-encode.php
我使用@Phil的解决方案 php.net/manual/en/function.var-export.php
不要忘记插入
开头并以
结尾的 PHP 脚本块?>
变量名称
在=
之后,您可以使用类似
";
I used @Phil's solution php.net/manual/en/function.var-export.php
Don't forget to insert
<?php
and ends with?>
variable name
following=
you can use something like
<?php \n\n\n\$schools=\n" . var_export($schools,true) . "\n\n\n?>";