PHP:获取数组(CSV)并智能返回信息

发布于 2024-10-27 13:52:00 字数 602 浏览 5 评论 0原文

嘿大家。我是第一次发帖,但我已经浏览过这个网站很多次了。我有一个编码问题,我不确定如何解决。首先,我将解释我需要做什么,以及我拥有哪些信息,我希望有人能在正确的方向上推动我。

我拥有的是一个电子表格 (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 技术交流群。

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

发布评论

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

评论(2

九命猫 2024-11-03 13:52:00

如果您想采用 CSV 方法,那么第一步是将文件读入二维数组:

$csv = array_map("str_getcsv", file("file.csv"));

现在这是一个索引数组,您需要知道哪一列是哪一列。但是,如果您知道城市始终位于 [2] 中,那么搜索其他信息就会变得简单:

foreach ($csv as $i=>$row) {
    if ($row[2] == "Chatsworth") {
        $zone = $row[0];
        $zip = $row[1];
        break;
    }
}

理想情况下,您会将其放入一个函数中,这样您就可以多次调用它。如果您可以配置要搜索的列,然后让它返回完整的找到的行,那将是最简单的。


好吧,如果您不知道 $city 名称在哪里,那么我会建议使用以下实用函数:

function search_csv($city) {
    global $csv;   // pre-parsed array (can be parameter though)

    foreach ($csv as $i=>$row) {
        if (in_array($city, $row)) {
            $result_rows[] = $row;
        }
    }
    return $result_rows;
}

function search_zip($city) {
    $rows = search_csv($city);
    foreach ($rows as $i=>$row) { 
        $rows[$i] = end(array_filter($row, "is_numeric"));
    }
    return $rows;
}

第一个返回匹配的 $rows 列表。我将让您自行决定如何确定哪一列包含哪一列。仅对于邮政编码,才有可能确定地返回结果。

If you want to pursue the CSV approach, then the first step is reading the file into a 2D array:

$csv = array_map("str_getcsv", file("file.csv"));

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:

foreach ($csv as $i=>$row) {
    if ($row[2] == "Chatsworth") {
        $zone = $row[0];
        $zip = $row[1];
        break;
    }
}

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:

function search_csv($city) {
    global $csv;   // pre-parsed array (can be parameter though)

    foreach ($csv as $i=>$row) {
        if (in_array($city, $row)) {
            $result_rows[] = $row;
        }
    }
    return $result_rows;
}

function search_zip($city) {
    $rows = search_csv($city);
    foreach ($rows as $i=>$row) { 
        $rows[$i] = end(array_filter($row, "is_numeric"));
    }
    return $rows;
}

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.

秋心╮凉 2024-11-03 13:52:00

H区,92603,欧文

H区,92604,欧文

J区,92625,科罗娜

等等

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

$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;
    }
}

最后你有一个结果数组,如下所示:

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

Zone H, 92603, Irvine

Zone H, 92604, Irvine

Zone J, 92625, Corona

etc.

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