使用 PHP 数组函数导入文本数据的想法

发布于 2024-09-14 10:23:36 字数 593 浏览 0 评论 0原文

我是 php 新手,需要一些编码帮助。我对 php 的经验很少,并且访问了 php.net 站点并阅读了几本书以获取有关如何执行此任务的一些想法。

似乎有很多功能,我很困惑什么才是最合适的。 (即 fgetcsv、explode()、regex?)用于提取文件中的数据。然后我需要帮助以有序的方式打印/显示这些信息。

这是我需要做的:

  • 导入,读取 txt 文件,即 分隔(参见示例)

属性并不总是有序的,并且某些记录会缺少属性。

  • 动态创建Web表(html) 呈现此数据

样本记录:

attribute1=value;attribute2=value;attribute3=value;attribute4=value;
attribute1=value;attribute2=value;attribute4=value;
attribute1=value;attribute2=value;attribute3=value;

我该如何处理?对此的最佳实践是什么?根据我的研究,我似乎会创建一个数组?多维?感谢您的时间和见解,我希望我的问题很清楚。

I am new to php and am asking for some coding help. I have little experience with php and have gone to the php.net site and read couple books to get some ideas on how to perform this task.

There seems to be many functions and I am confused on what would be the best fit. (i.e. fgetcsv, explode(), regex??) for extracting data in the file. THen I would need assistance printing/display this information in orderly fashion.

Here is what I need to do:

  • import, readin txt file that is
    delimited (see sample)

The attributes are not always ordered and some records will have missing attributes.

  • Dynamically create a web table (html)
    to present this data

Sample records:

attribute1=value;attribute2=value;attribute3=value;attribute4=value;
attribute1=value;attribute2=value;attribute4=value;
attribute1=value;attribute2=value;attribute3=value;

How do I go about this? What would be best practice for this? From my research it seems I would create an array? multidimensional? Thank you for your time and insight and i hope my question is clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

陌生 2024-09-21 10:23:36

看起来像家庭作业,如果是的话最好将其标记为家庭作业。

您需要查看 file()foreach()explode() 鉴于它是由 ; 分隔的

如果属性丢失,属性的数量应该没有关系,但这一切都取决于您如何设置显示数据。鉴于它们丢失了,您将需要知道最大数量的属性是多少,才能正确设置表而不引起问题。

祝你好运!

Seems like homework, if so best to tag it as such.

You will want to look into file(), foreach() and explode() given that it is delimited by ;

The number of attributes should not matter if they are missing, but all depends on how you setup the display data. Given that they are missing though, you will need know what is the largest amount of attributes to setup the table correctly and not cause issues.

Best of luck!

ま柒月 2024-09-21 10:23:36

我首先使用 file() 方法,它会给你一个数组,其中每一行作为一个元素。然后进行几次爆炸和循环以完成所有操作,首先在“;”上爆炸,然后循环遍历每个爆炸并在“=”上爆炸。

i would first use the file() method, which will give you an array with each line as an element. Then a couple of explodes and loops to get through it all,first exploding on ';', then loop through each of these and explode on '='.

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