Problems with array_search and file()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

Problems with array_search and file()

Post by jaymoore_299 »

anyone else have problems using array_search to seek in arrays created by file()?

I have tried creating my own array and I had no problems getting the correct location of the first instance, however when I used a file() array, it fails.

The file in question isn't the problem. To test this I used a file that I successfully loaded just before, and printed the output. file() is reading the file, but array_search does not seem to work with that array. Other arrays seem to work fine.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

have you tried some of the other array functions on the array created by file()?

could also be that the values created in the array are larger than the scope you're searching as I think file() generates new keys based on new lines...
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

File returns an array based on the entire line, including newline... which is why you cannot search and find anything... I'm not sure if it uses CRLF on windows though or if it's faked.
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

Post by jaymoore_299 »

I'm trying to find a way to find the first instance of a certain string pattern, such as "####" in a file.
I can get the file contents easily with file(), but is there a way that I can change that array to work with array_search, or use some other method to get a line from a file one by one to process?
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

forach-loop the contents and search using strpos or so (and perhaps trim of the rightmost character if it is a newline).
Post Reply