PHP: Taking Array (CSV) And Intelligently Returning Information?

You can take the file and get all its contents. Then split it up by new line.

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($col2) == $searchCity){ $results = $col; } } and at the end you 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'), ).

– Brian Driscoll Mar 28 at 18:18 @Brian so explode by that ^_^ – Neal Mar 28 at 18:18 @Neal I think you're missing the point. You've assumed by your solution that OP knows what the newline encoding will be. What if that's not the case?

– Brian Driscoll Mar 28 at 18:20.

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 ($row2 == "Chatsworth") { $zone = $row0; $zip = $row1; 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.

Thanks, this seems to be a step in the right direction.. But let's say I don't know which row the city is in (which happens to be the case). Would I just replace the numbers in the 's with $i, so it searches the current line? I don't know where the city is so I must search for it.

And the information I need will be in the same row as the city. However, some rows have the same city name, with a different zip code. So I need to grab the different zip codes which are in different rows, and apply them to that cityname.

Maybe I could just do something like $zip . = $row . ", ";?

– Kevin Mar 28 at 18:35 @Kevin: The other way round. To collect an array of zip codes you would use $zip = $row1;. But if you don't know which column the city name is in, then you probably won't know in advance which one holds the numeric zip code, right?

The searching issue can be solved, but the finding the right result is a showstopper. – mario Mar 28 at 18:39 1 Can't you just do foreach ($row as $key => $col) {if ($col == "Chatsworth") { /* $key is the index where the city is. */ }}?

– Bruno De Barros Mar 28 at 18:48 @Bruno: True. Come to think of it $key = array_search("Chatsworth", $row); might also work. – mario Mar 28 at 18:52 I'm going to try this out.. thank you so much for your help, I sincerely appreciate it.

– Kevin Mar 28 at 19:05.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions