PHP: Taking Array (CSV) And Intelligently Returning Information

北慕城南 提交于 2020-01-17 00:53:30

问题


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.


回答1:


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'),
)



回答2:


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.



来源:https://stackoverflow.com/questions/5463168/php-taking-array-csv-and-intelligently-returning-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!