PHP - 在数组中使用 include() 函数
如何在数组中使用 include() 函数?
例如,我有一个列出了一堆县的数组,我没有将所有县输入到一个数组中,而是创建了一个以逗号分隔的县的 .txt 文件。从逻辑上讲,我认为它应该这样工作:
array (include ("counties.txt"));
但它在数组函数之外生成列表。
是否有在数组中使用 include() 函数的不同方法?
How would I use the include() function within an array?
For example, I have an array which lists a bunch of counties, instead of inputting all of the counties into an array, I created a .txt file of comma delimited counties. Logically, I think it's supposed to work as such:
array (include ("counties.txt"));
But it produces the list outside of the array's function.
is there a different method of using the include() function within an array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想你只想使用
file('countries.txt', FILE_IGNORE_NEW_LINES);
I think you just want to use
file('countries.txt', FILE_IGNORE_NEW_LINES);
尝试
file_get_contents()
Try
file_get_contents()
我会尝试 readfile(),在新行上分隔每个县。 readfile() 函数将文件的每一行放入索引数组中。
I would try readfile(), separating each county on a new line. The readfile() function places each line of the file into an indexed array.
一种方法是让
counties.txt
文件具有以下格式:然后,只需将其包含在数组中,如下所示:
另一种方法是解析
counties.txt
,如下所示:方式有效并且会产生相同的结果。
One way is for your
counties.txt
file to have the following format:Then, just include it in your array like:
Another method is to parse
counties.txt
like:Either way works and will produce the same result.