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
https://forums.phpfreaks.com/topic/222873-in_array-is-puzzling-me/
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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.