function csv_data_to_zip_array($csv)
{
$f = fopen($csv, "r");
$i= 0;
while($line= fgets($f)){
$line = preg_replace("/[^0-9]/", "", $line);
if(is_numeric($line)&&strlen($line)==5){ // it is a zip code
$array[] = $line;
}
$i++;
}
fclose($f);
return $array;
}
That is my function, it’s reading a large csv with a bunch of zip codes into an array.
It’s what Ignacio said: either the
whileorifis always returning false, and you should initialize$arrayas an empty array before that while loop anyway.Here is a suggestion for maybe getting results faster: If the CSV always contains the ZIP codes in the same column, use
fgetcsvinstead. You can then just specify the column number to check (is_numeric(substr($line[column_num],0,5))) rather than running apreg_replaceon the whole line.Edit:
Using the sample data you posted from your website (I noticed all data files in there have the zipcode in the first column; some files have a header row and some do not), this function does the trick: