0

I'm trying to compare 2 values, one coming from post data and the other one coming from an array, the weird thing is, when I compare them, all of the records show that they are not equal but some of the values have equal values:

What I actually need to do is to unset those values that are not equal in the post data sent.

$a = $_POST['time']; (Value is 01:03)

$testarray = array("12:30","01:03","03:30");

for($x = 0; $x < count($testarray);$x++){
  if($a === $testarray[$x]){
    echo "ok";
  }
  else
  {
    echo "not";
  }
}

All of the my results are showing not, even though there is a similar value on one of the contents in the array.

What seems to be the problem here? I've check the values and the data types are both string.

1
  • 2
    if(in_array($a, $testarray)). no need of the loop. Commented Sep 15, 2015 at 6:34

2 Answers 2

1

use in_array

$a = $_POST['time']; //(Value is 01:03)

$testarray = array("12:30","01:03","03:30");


if( in_array($a, $testarray))
{
    echo "ok";
}
else
{
    echo "not";
}
2
  • this will still work even if the array is a multidimensional one right? Commented Sep 15, 2015 at 6:54
  • ya. Its search entire array Commented Sep 15, 2015 at 6:54
1

Simple use in_array to Checks if a value exists in an array

 $a = $_POST['time'];
 $testarray = array("12:30","01:03","03:30");

if (in_array($a, $testarray))
  {
  echo "Match found";
  }
else
  {
  echo "Match not found";
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.