Getting data from text file and display it in html table(从文本文件中获取数据并将其显示在 html 表中)
问题描述
我为每一行得到了一个这种模式的文本文件:
I got a text file in this pattern for each line:
我正在尝试用它创建一个记分牌.
I'm trying to create a scoreboard out of this.
这是我的尝试:
问题是如何将每个 username
和 score
复制到表中(没有 :
字符)?
The question is how can I copy each username
and score
to the table (without the :
character) ?
我当前的 php 代码:
My current php code:
这只会给我第一个用户名,但我想获取每一行的所有用户名.
This will give me only the first username, but I want to get all the usernames from every line.
推荐答案
这应该适合你:
这里我首先使用 file()
其中每一行都是一个数组元素.在那里我忽略每行末尾的空行和换行符.
Here I first get all lines into an array with file()
where every line is one array element. There I ignore empty lines and new line characters at the end of each line.
在此之后,我使用 array_map()并使用
explode(),然后我将其作为数组返回以创建一个多维数组,例如:
After this I go through each line with array_map()
and extract the username + score with explode()
, which I then return as array to create a multidimensional array, e.g:
我用 usort()
(要将顺序从 ASC
更改为 DESC
,您只需将 >
更改为 <
> 在 usort()
) 之后,我简单地遍历数据并将其打印在表格中.
The I sort the array by the score with usort()
(To change the order from ASC
to DESC
you can just change >
to <
in usort()
) and after this I simply loop through the data and print it in the table.
输出:
这篇关于从文本文件中获取数据并将其显示在 html 表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!