Jump to content

in_array is puzzling me


soma56

Recommended Posts

I know this works and that's why I'm puzzled. I'm bringing in data from a textarea and trying to compare it.

 

index.php

<form name"fruit-farm" action="fruit-check.php" method="POST">
<textarea name="fruit-list">
Apples
Bananas
Oranges
Pickles
Hamburgers
Grapes
</textarea>
<input type"submit" name="submit" value="Check The Fruit"> 
</form>

 

fruit-check.php

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

//Bring in the data
$fruit-list = explode("\n", $_POST['fruit-list']); 

//Search for Hamburgers
$hamburgers = "Hamburgers";

if (in_array($hamburgers,$fruit-list)){
 echo "That is not a fruit";
} 

}

 

What's puzzling me is that it's not working. It must be something blantently obvious. It has something to do with the '$fruit-list' array and how it is being brought into the form. It posts fine. The reason I think this is the case is because if I simply create an array that is identical to the form coming in it works:

 

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

//Bring in the data
$fruit-list = explode("\n", $_POST['fruit-list']); 

//Search for Hamburgers
$hamburders = "Hamburgers";

$fruit-list = array('Apples', 'Bananas', 'Oranges', 'Pickles', 'Hamburgers', 'Grapes);

if (in_array($hamburders,$fruit-list)){
 echo "That is not a fruit";
} 

}

 

When I 'print_r' both arrays they both look identical. What is wrong and/or different with the way I'm bringing in the textarea that's causing this not to work?

Link to comment
Share on other sites

Thanks guys. Ironically there is not hyphens - I just renamed everything to present a simply and logical example for everyone to see.

 

In any event you got me thinking about trimming the whitespace which led to the discovery of a solution.

 

Instead of exploding the array with '\n' I tried '\r' which magically solved the problem :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.