PHP:获取数组(CSV)并智能返回信息
嘿大家。我是第一次发帖,但我已经浏览过这个网站很多次了。我有一个编码问题,我不确定如何解决。首先,我将解释我需要做什么,以及我拥有哪些信息,我希望有人能在正确的方向上推动我。
我拥有的是一个电子表格 (CSV),其中包含以下信息:区域名称、邮政编码、城市名称。一个区域应该有许多属于它的城市,并且每个城市很可能有很多属于它的邮政编码。例如:
H区,92603,欧文
H区,92604,欧文
J区,92625,科罗娜
等等
好吧,现在这已经不碍事了,这就是我需要处理此信息的事情。我需要能够输入一个城市名称,并让它返回该城市下的所有邮政编码以及该城市所在的区域。例如,如果我输入查茨沃斯,它应该给我(区域X) 和 (12345, 12346, 12347) 作为邮政编码(仅作为示例)。
我不确定解决这个问题的最佳方法。我可以创建一个 MySQL 数据库并从那里开始工作,或者仅从 .csv 文件开始工作,或者将其硬编码到 PHP 文件中。我不知道如何在数组列中搜索值,然后相应地返回其他列(特别是每个城市有多个邮政编码)。
如果有人可以帮助我,我将不胜感激。另外,如果您需要我提供更多信息,请随时告诉我。预先感谢大家的阅读。
Hey Everyone. I'm a first time poster, but I've browsed this site a number of times. I have a coding issue that I'm not sure exactly how to solve. First I'll explain what I need to do, and what information I have, and I hope somebody can give me a nudge in the right direction.
What I have is a spreadsheet (CSV) that has the following info: Zone Name, Zip Code, City Name. One zone should have many cities that fall under it, and every city most likely has many zip codes that fall under it. For example:
Zone H, 92603, Irvine
Zone H, 92604, Irvine
Zone J, 92625, Corona
etc.
Okay, now that that's out of the way, here's what I need to do with this info. I need to be able to input a city name and have it return to me all zip codes that fall under that city, as well as the zone that the city lies in. For example, if I input Chatsworth, it should give me (Zone X) and (12345, 12346, 12347) as the zip codes (just an example).
I'm not sure the best way to go about this. I could create a MySQL database and work from there, or just work from .csv files, or hardcode it into the PHP file. I don't know how to search for a value in an array column, and then return the other columns accordingly (especially with multiple zip codes per city).
If anybody can help me out, it would be greatly appreciated. Also, feel free to let me know if you need more information from me. Thanks in advance to everyone reading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想采用 CSV 方法,那么第一步是将文件读入二维数组:
现在这是一个索引数组,您需要知道哪一列是哪一列。但是,如果您知道城市始终位于
[2]
中,那么搜索其他信息就会变得简单:理想情况下,您会将其放入一个函数中,这样您就可以多次调用它。如果您可以配置要搜索的列,然后让它返回完整的找到的行,那将是最简单的。
好吧,如果您不知道 $city 名称在哪里,那么我会建议使用以下实用函数:
第一个返回匹配的 $rows 列表。我将让您自行决定如何确定哪一列包含哪一列。仅对于邮政编码,才有可能确定地返回结果。
If you want to pursue the CSV approach, then the first step is reading the file into a 2D array:
Now this is an indexed array, where you need to know which column is which. But if you know the city is always in
[2]
then searching for the other information becomes simple:Ideally you would put this into a function, so you can call it multiple times. It would be easiest if you make it configurable which column to search, and just have it return the complete found row.
Okay so if you don't know where the $city name is in, then I would propose following utility function:
The first one returns a list of $rows which match. I'll leave it up to you how to figure out which column contains which. Only for the zip code it's kind of possible to return the results deterministically.
您可以获取该文件并获取其所有内容。然后用新行将其分割:
最后你有一个结果数组,如下所示:
you can take the file and get all its contents. then split it up by new line:
and at the end u have an array of the results like this: