Jump to content

search for something inside array


gsmith
 Share

Recommended Posts

I am currently reading a file into an array:

$newthing=file(filename);

each line I read in will be of the general format:$uname[0]=username; $known_as[0]=realname;$uname[1]=username; known_as[1]=realname;etc.I have another variable, we'll call it $x. $x will equal the username portion of one of the above lines. I want to do other things (that I do know how to do) ONLY IF $x is NOT in the line that I've read into the $newthing array. How can I search and determine this line by line?

Link to comment
Share on other sites

Before your response, I had searched PHP.net.I tried using strpos()

foreach($temp as $value)    {      if(!(strpos($value, $usr)))      {        fwrite($fs, $value);      }    }

$temp is the array I got using the file() command. I want to write to the file $fs all the lines in $temp, except for the line that contains within it somewhere the same exact value that $usr holds.The above code has worked if the line in question is the last line of the file. But if it's in the middle, this code stops writing the remaining lines back to the file once the one line in question is found.

Link to comment
Share on other sites

Try generating a string and then write the string to the file:

$output = '';foreach($temp as $value)   {     if(!(strpos($value, $usr)))     {       $output .= "$value\n";     }   }fwrite($fs, $output);

[edit] fixed the typos[/edit]-hs

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...