Forum : How can I check if a value is already in an array?
Brief description  about Online courses   join in Online courses
View Abhijit  Paul 's Profile

How can I check if a value is already in an array?

Hi,

Can any one tell me how to check if a value is already in an array
Asked by Abhijit Paul | Nov 27, 2009 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
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!
Nov 27, 2009