nickf1195 Posted July 25, 2008 Share Posted July 25, 2008 Hey everyone, I have a problem here that I can't seem to figure out (that would explain why i'm posting here ). I have this code: <?php function select_csv($file,$delimiter,$field,$query) { $data_array = file($file); for ( $i = 0; $i < count($data_array); $i++ ) { $parts_array[$i] = explode($delimiter,$data_array[$i]); if(trim(strtolower($parts_array[$i][$field])) == trim(strtolower($query))) { $result_array[] = $parts_array[$i]; } } return $result_array; } echo '<p>'; $data = select_csv('data.csv','|','0','demo-query'); for ( $i = 0; $i < count($data); $i++ ) { for ( $u = 0; $u < count($data[$i]); $u++ ) { echo $data[$i][$u].' '; } echo '<br>'; } ?> I have a csv file with two columns. This script accepts a query and displays every record whose first column is equal to that query (demo-query above.) I'd like it to just return the content of the second column. I've been trying for hours and can't seem to figure this one out. Any level of help is greatly appreciated. Nick Quote Link to comment Share on other sites More sharing options...
TransmogriBenno Posted July 25, 2008 Share Posted July 25, 2008 Do you know what part of the code is the problem? Have you checked that the result from file() is what you want, and that $parts_array[$i] = explode($delimiter,$data_array[$i]); is doing what you expect it to? Quote Link to comment Share on other sites More sharing options...
nickf1195 Posted July 25, 2008 Author Share Posted July 25, 2008 So $parts_array is the result of explode, so each item in that array is both fields for every record? Help. :-\ Thanks, Nick Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.