如何在PHP中读取/写入结构化数据?

发布于 2025-01-04 21:25:44 字数 553 浏览 0 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

守望孤独 2025-01-11 21:25:44

我想到了一些事情:

  • 序列化,例如 file_put_contents($file, serialize($schools))
  • sqlite,它不是平面文件,但很容易上手 - 你只需要将 PDO 指向您的数据库文件,然后就可以开始了。您可以通过 CLI 和基于 Web 的工具进行编辑。

编辑:啊,如果你需要通过文本编辑器进行编辑,那就不太好了。您可以改为使用 PHP 中的格式 -

return 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"
    ),
  );

然后您可以将其弹出到文件中并执行 $schools = require($schools) ,它将很好地导入。我将其用于数据固定装置,效果非常好。

Couple of things come to mind:

  • Serialisation, e.g. file_put_contents($file, serialize($schools))
  • sqlite, which isn't a flat file, but is very easy to get started with - you just need to point PDO at your database file, and off you go. You can edit via CLI and web-based tools.

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 -

return 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"
    ),
  );

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.

淡看悲欢离合 2025-01-11 21:25:44

好吧,如果您必须手动编辑文件,那么很难击败 YAML,因为该格式可能与您将要遇到的(至少在这个星球上)最接近“一种文件格式设计是为了便于手动编辑。”

您对“使用 PHP 数组变量”的思考在这里实际上是无关紧要的,因为这样的概念仅适用于程序的操作......在读取数据之后 > 并在随后将数据写回之前。它们与您决定使用的格式没有任何关系。

我建议您弄清楚什么数据表示对于人类来说最容易处理,然后围绕它编写计算机程序。此外,由于存在人类搞砸某些东西的可能性,因此程序应该不信任其输入。如果遇到垃圾,它应该“亲切地、有意义地、外交地”做出响应,例如:

  • “哎呀,我根本无法读取该文件。”
  • “等一下:该条目应该以三个或四个字符的大写名称开头,例如 WAC 或 PACS,但在第 123456 行却不是。”
  • “等一下:该条目应该至少包含以下键:“full_name”、“version”和“etc”。但第 567891 行没有这样做。
  • “因为我刚刚在此输入文件中发现了以下 5 个错误,如之前所列,我特此拒绝处理其中的任何部分。(因此,我没有只是对您的数据库进行了处理。)”

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:

  • "Oops, I simply couldn't read that file, at all."
  • "Wait a minute: this entry is supposed to begin with a three or four-character upper case name, such as WAC or PACS, but at line 123456 it doesn't."
  • "Wait a minute: this entry is supposed to contain at least the following keys: "full_name," "version," and "etc." But line 567891 doesn't do that."
  • "Because I just found the following 5 errors in this input file, as previously listed, I hereby decline to process any part of it. (Therefore, I didn't just hose your database.)"
ヅ她的身影、若隐若现 2025-01-11 21:25:44

好吧,您可以尝试 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

柠檬 2025-01-11 21:25:44

我使用@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

  • a PHP scripting block that starts with <?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?>";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文