Match name of location with zip code from tab seperated TXT file using PHP

143
March 13, 2018, at 08:50 AM

I have a form that the user fills out and they need to enter their zip code. I would therefore like to retrieve the location name of the zip code from a large TXT file containing tab-separated data with zip codes and location names. The zip code is on column 1, while location name is on column 2.

2058    SESSVOLLMOEN    0235    ULLENSAKER  G
2060    GARDERMOEN  0235    ULLENSAKER  G
2061    GARDERMOEN  0235    ULLENSAKER  P
2062    JESSHEIM    0235    ULLENSAKER  P
2063    JESSHEIM    0235    ULLENSAKER  G
2066    JESSHEIM    0235    ULLENSAKER  G
2067    JESSHEIM    0235    ULLENSAKER  G
2068    JESSHEIM    0235    ULLENSAKER  G
2069    JESSHEIM    0235    ULLENSAKER  G

My $input contains '2069'. How can I get the city/location name of $input?

Answer 1

It would've have helped if you provided some examples of what you've already tried and where you're stuck. I'm assuming you're fairly new to php and probably don't know where to look. Here is a piece of code that should work for you:

function searchForValue($val, $table_array) {
   foreach ($table_array as $row_string) {
       $row_array = explode ("\t", $row_string);  //this is a single row converted into a one dimensional array 
       if ($row_array[0] == $val) {
           return ($row_array[1]) ; //second column in the row that contains the city name
           break;
       }
   }
   return null;
}
//Read the file contents, assuming file is in the same directory  
$text = file_get_contents('SampleText.txt', true);
//Convert the string with all file contents to an array of strings, where each element is a string value corresponding to a row in the file 
$zip_table_array = explode("\r\n", $text);
$zip_to_search = '2069'; 
$city = searchForValue($zip_to_search, $zip_table_array);
echo ("<br>City for zip $zip_to_search is: " . $city ) ; 

Hope this helps.

READ ALSO
htaccess rewrite rules to force a 404 up one directory levek while maintaining the request URL

htaccess rewrite rules to force a 404 up one directory levek while maintaining the request URL

I am working on a website that has many directories and subdirectoriesDeveloping locally, I am running php's built in webserver

134
Parsing emoticons into emojis - is there a list of common ones? [on hold]

Parsing emoticons into emojis - is there a list of common ones? [on hold]

Using PHP I would like to parse a string of text and replace any emoticons with Unicode emoji charactersFor example, ":)" would be replaced with "

118
MySQL DELETE FROM using DATEDIFF works from console but not in PHP?

MySQL DELETE FROM using DATEDIFF works from console but not in PHP?

I have TABLE(s) with a datetime field called "Lastcall"I need to delete any records in these tables which are more than (3) days old

127
htop shows many sleeping MySQL threads

htop shows many sleeping MySQL threads

When I look in htop, it shows me the following overviewPlease note that they are all sleeping (mode S)?

131