Page 1 of 1
Array search
Posted: Tue Aug 16, 2005 12:31 pm
by spacebiscuit
I am using the function array_search to look for a string in an array:
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):
The output is:
b
Cannot see why the match is not being evaluating to true.
Any suggestions?
Thanks,
Rob.
Solution
Posted: Tue Aug 16, 2005 12:36 pm
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.
Posted: Tue Aug 16, 2005 12:52 pm
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.
Posted: Tue Aug 16, 2005 12:55 pm
by pickle
Is there any chance that $list[4] may contain some whitespace?
Posted: Tue Aug 16, 2005 12:56 pm
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:
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?
Posted: Tue Aug 16, 2005 1:09 pm
by spacebiscuit
My output gives me........
--b __--
Do I need to strip first?
Rob.
Posted: Tue Aug 16, 2005 1:13 pm
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.
Posted: Tue Aug 16, 2005 1:14 pm
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.
Posted: Tue Aug 16, 2005 1:15 pm
by nielsene
if its OK to change the array:
should do it.
Posted: Tue Aug 16, 2005 3:43 pm
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.