PHP:采取数组(CSV)和智能返回信息 [英] PHP: Taking Array (CSV) And Intelligently Returning Information

查看:125
本文介绍了PHP:采取数组(CSV)和智能返回信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿大家。我是第一次海报,但我浏览了这个网站多次。我有一个编码问题,我不知道确切如何解决。首先,我将解释我需要做什么,以及我有什么信息,我希望有人能够向我轻推一个正确的方向。

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.

我有什么具有以下信息的电子表格(CSV):区域名称,邮政编码,城市名称。一个区域应该有许多城市落在它下面,每个城市最可能有很多邮政编码属于它。例如:

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, 92603, Irvine

Zone H,92604 ,Irvine

Zone H, 92604, Irvine

Zone J,92625,Corona

Zone J, 92625, Corona

等。

好吧,现在这是不对的,这里是我需要做的这个信息。我需要能够输入一个城市名称,并返回我所有的城市,以及该城市所在的区域的邮政编码。例如,如果我输入Chatsworth,它应该给我(区域X)和(12345,12346,12347)作为邮政编码(只是一个示例)。

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).

我不确定最好的办法。我可以创建一个MySQL数据库,并从那里工作,或只是工作从.CSV文件,或硬编码到PHP文件。我不知道如何在数组列中搜索值,然后相应地返回其他列(特别是每个城市有多个邮政编码)。

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.

推荐答案


Zone H,92603,Irvine

Zone H, 92603, Irvine

Zone H,92604,Irvine

Zone H, 92604, Irvine

Zone J,92625,Corona

Zone J, 92625, Corona

等。

您可以获取文件并获取其所有内容。然后用新行拆分:

you can take the file and get all its contents. then split it up by new line:

$searchCity = 'Searched'; //or whatever city you are looking for
$file = file_get_contents('file.csv');
$results = array();
$lines = explode("\n",$file); 
//use any line delim as the 1st param,
//im deciding on \n but idk how your file is encoded

foreach($lines as $line){
    //split the line
    $col = explode(",",$line);
    //and you know city is the 3rd element
    if(trim($col[2]) == $searchCity){
         $results[] = $col;
    }
}

在结尾有一个结果数组像这样:

and at the end u have an array of the results like this:

$results = array(
   array('Zone B', '12345', 'Searched'),
   array('Zone Z', '35145', 'Searched'),
   array('Zone Q', '12365', 'Searched'),
)

这篇关于PHP:采取数组(CSV)和智能返回信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆