Array search

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
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Array search

Post by spacebiscuit »

I am using the function array_search to look for a string in an array:

Code: Select all

$result=array_search('b', $list);

If there is a match $result should be given the value of the index key. I have checked that the string is in the array. I am testing the result as below (outputting the index of the array I know to hold the value I am looking for):

Code: Select all

echo"$list[4] $result";
The output is:

b

Cannot see why the match is not being evaluating to true.

Any suggestions?

Thanks,

Rob.
biznickman
Forum Newbie
Posts: 13
Joined: Tue Aug 16, 2005 10:32 am

Solution

Post by biznickman »

As stated on php.net the function array_search: "Searches the array for a given value and returns the corresponding key if successful"

This is why it is returning "b";

If you used this in a conditional statement, e.g.

if( array_search('b', $list) )
echo "Found b";

This would work as if it were a boolean.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

If it is returning the key then it should be returning the value 4 in result. 'b' is not being returned, this is just my test output when I am echoing the 4th index of the array. Something is not working and I think you have misread my posting.

Thanks,

Rob.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Is there any chance that $list[4] may contain some whitespace?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Some things to check, try a print_r($list) before and after the array_search, just to make sure that the list is what you think it is. When debugging I like to add delimiters to my print statemens something like:

Code: Select all

echo "--{$list[4]}__$result--";
So to make sure which piece is generrating output. But in this case I think you're correct that result is getting messed up not the list.

Do you have error_reporting turned on?

Is there any other code between the two lines you showed in your post?
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

My output gives me........

--b __--

Do I need to strip first?

Rob.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

robburne wrote:My output gives me........

--b __--

Do I need to strip first?

Rob.
Yeah, before its added to the array, or you can try lopping over the array and using strstr, preg_match, etc, if you can't modify the array.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

It seems there is a space after the b hence the non-match.

How can I strip whitespace from each index of the array?

Rob.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

if its OK to change the array:

Code: Select all

$list = array_map("trim",$list);
should do it.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Yeah that worked fine thanks.

Here is the complete code in case anyone is interested. Basically a text file of email addresses is maintained alphabetically and via the script you can insert a new email which in turn is inserted in the correct place.

Code: Select all

<?

$email=$_POST["email"];

if (isset($_POST['submit'])) {

   $email=strtolower($email);

   $list=file("email_list.txt");
   
   $list=array_map("trim",$list);
   
   if (array_search($email, $list)) {
      echo"<p>The email address you have input already exists.</p>";
      ?>
      <form action="<?$PHP_SELF;?>" method="POST">
      <p><input type="submit" value="Back" name="submit2" align="left"></p>
      </form>
      <?
                                    }
                                    
   else{
   
      $handle=fopen("email_list.txt","a+");

      fwrite($handle, "$email\r\n");

      fclose($handle);

      $list=file("email_list.txt");

      sort($list);

      $handle2=fopen("email_list2.txt","x+");

      foreach($list as $val){
                    fwrite($handle2, $val, strlen($val));
                            }

      fclose($handle2);
   
      unlink("email_list.txt");
   
      rename("email_list2.txt","email_list.txt");
      
      echo"<p>The email address '$email' has successfully been added.</p>";
      
      ?>
      <form action="<?$PHP_SELF;?>" method="POST">
      <p><input type="submit" value="Another" name="submit2" align="left"></p>
      </form>
      <?
      
        } // close else

                            } // close submit
                            
else{

    ?>
    <form action="<?$PHP_SELF;?>" method="POST">
    <p>Please enter an email address :<input type=text name=email size=30></p>
    <p><input type="submit" value="Submit" name="submit" align="left"></p>
    </form>
    <?

    }

?>
Thanks,

Rob.
Post Reply