Neopyros Posted August 7, 2007 Share Posted August 7, 2007 I have a simple problem: Using fgetcsv in my file to split the "," now i have arrays inside an array. I want to search for a word, how can i return the positions of where the words were found? Heres my code so far so small =( <?php $handle = fopen("JobModLog.txt", "r"); $search = "Date Time"; /*------------------------------------------------------------------------------------------------------*/ for($i=0;$i<=!feof($handle);$i++){ $data[$i] = fgetcsv($handle,1000, ","); } fclose($handle); /*------------------------------------------------------------------------------------------------------*/ Any suggestions? Newbie question, i know ._. Thks Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 7, 2007 Share Posted August 7, 2007 <?php $handle = fopen("JobModLog.txt", "r"); while (!feof($handle)) { $data[] = fgetcsv($handle,1000, ","); } fclose($handle); $searchWord = "test"; foreach ($data as $rowKey => $line) { foreach($line as $columnKey => $item { if (strpos ($item, $searchWord)) { echo "The search word exists on row $rowKey, column $columnKey <br>"; } } } ?> Quote Link to comment Share on other sites More sharing options...
Neopyros Posted August 7, 2007 Author Share Posted August 7, 2007 Sorry but i didn't understand.... When you do this: foreach ($data as $rowKey => $line) { foreach($line as $columnKey => $item){ if(strpos($item, $searchWord)) { echo "The search word exists on row $rowKey, column $columnKey <br>"; } } } Return an error message Warning: Invalid argument supplied for foreach().... Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 7, 2007 Share Posted August 7, 2007 <?php if ($handle = fopen("test.txt", "r")) { while (!feof($handle) && $xxx<20) { $data[] = fgetcsv($handle,1000, ","); } fclose($handle); $searchWord = "test"; //Iterrate through top level of array foreach ($data as $rowKey => $line) { //Itterate through the 2nd level of the array foreach($line as $columnKey => $item) { //Check if the search string exists in the data item if (strpos ($item, $searchWord)!==false) { echo "The search word exists on row $rowKey, column $columnKey <br>"; } } } } else { echo "unable to open file"; } ?> Quote Link to comment Share on other sites More sharing options...
Neopyros Posted August 7, 2007 Author Share Posted August 7, 2007 Still not working it can't itterate in the 2nd array... and why the xxx<20? Invalid argument supplied for foreach() Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 7, 2007 Share Posted August 7, 2007 Woops, I had that for testing to prevent infinite loops. That can be removed. Otherwise, I tested the code and it worked perfectly. Here are the two files I used to test and the results: test.txt (csv file) apple, orange, pear grape, apple, bannana apricot, guava, pineapple test.php (php file) <?php if ($handle = fopen("test.txt", "r")) { while (!feof($handle)) { $data[] = fgetcsv($handle,1000, ","); } fclose($handle); $searchWord = "apple"; //Iterrate through top level of array foreach ($data as $rowKey => $line) { //Itterate through the 2nd level of the array foreach($line as $columnKey => $item) { //Check if the search string exists in the data item if (strpos ($item, $searchWord)!==false) { echo "The search word '$searchWord' exists on row $rowKey, column $columnKey: $item<br>"; } } } } else { echo "unable to open file"; } ?> Results: The search word 'apple' exists on row 0, column 0: apple The search word 'apple' exists on row 1, column 1: apple The search word 'apple' exists on row 2, column 2: pineapple Quote Link to comment Share on other sites More sharing options...
Neopyros Posted August 7, 2007 Author Share Posted August 7, 2007 Heres a part of my file search word = Date Time ***** Client Settings (15/12/2006 17:49:52) ***** Unit : mm Coordinate Display : PanelView Display using same origin settings for bottom and side coordinates : TRUE Panel Origin Location : Lower Left Panel Origin Offset X : 0.00 Panel Origin Offset Y : 0.00 Board Origin Location : Lower Left Board Origin Offset X : 0.00 Board Origin Offset Y : 0.00 ************************************************** Date Time, Modified User, Command, Data Type, Previous Data, Modified Data And the error keep going =P Edit: Somebody here asked if the problem cause isn't cause $line isn't an array... i couldn't answer it... is it right? Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 7, 2007 Share Posted August 7, 2007 If I am understanding you correctly the "code" you posted above is your "JobModLog.txt" file and your search term is "Date Time". when I added that to my data file and changed the search term I ended up with this result: The search word 'Date Time' exists on row 12, column 0: Date Time Everything seems to work fine for me. However a blank line at the end of the input can cause an error, so some error handling to verify that the $line is an array is a good idea: foreach ($data as $rowKey => $line) { if (is_array($line)) { //Itterate through the 2nd level of the array foreach($line as $columnKey => $item) { //Check if the search string exists in the data item if (strpos ($item, $searchWord)!==false) { echo "The search word '$searchWord' exists on row $rowKey, column $columnKey: $item<br>"; } } } } Quote Link to comment Share on other sites More sharing options...
Neopyros Posted August 7, 2007 Author Share Posted August 7, 2007 Man, i can't believe it, there was a empty line in my file. Sorry all the bother Thanks a lot!!! 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.