Jump to content

Searching an Array


khalek

Recommended Posts

Hello all!

I am working on a php code that will accept user input and then search an array for that number. If its there, i want it to display yes, and if not, no. this is the code i have but it will only come up with yes for any number....PLEASE HELP!

CODE FOR FORM ON PAGE 1

<form action="Ex4-04-test.php" method="get">

Pick a number between 0 - 50:<br />

<input name="num_wanted" type="text" id="num_wanted" />

<input name="go" type="submit" value="Search for my Number!" />

</form>

CODE FOR SEARCHING ARRAY

 

<?php

$numWanted = $_POST['num_wanted'];

// create an array of numbers called $numArray

$numArray = array(12, 0, 89, 64, 23, 50);

?>

<?php

// use the array_search function to check if the number entered is in the array

      if(in_array($numWanted, $numArray) )

      {

        echo "YES";

      }

      else

  {

        echo "NO";

      }

?>

if someone could please help me that would be really great :D

Link to comment
https://forums.phpfreaks.com/topic/119944-searching-an-array/
Share on other sites

Try

<?php
$numWanted = trim($_POST['num_wanted']);
// create an array of numbers called $numArray
$numArray = array(12, 0, 89, 64, 23, 50);
?>
<?php
// use the array_search function to check if the number entered is in the array
      if(in_array($numWanted, $numArray) )
      {
        echo "YES";
      }
      else
     {
        echo "NO";
      }
?>

Link to comment
https://forums.phpfreaks.com/topic/119944-searching-an-array/#findComment-617902
Share on other sites

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.