How can I check if a value is already in an array?
By Abhijit Paul
Hi,
Can any one tell me how to check if a value is already in an array
Teacher SiliconIndia replied to Abhijit Paul Friday, November 27, 2009
Hi paul,
Here is an example of in_array in action:
<?php
$values = array("banana","apple","pear","banana");
$newvalue = "pear";
if (in_array($newvalue,$values)) { echo "$newvalue is already in the array!"; }
?>
The most common mistake made with in_array is to pass the arguments in the wrong order to the function, so take care!