How can I check if a value is already in an array?
Answers were Sorted based on User's Feedback
Answer / mohammed khalid khan
The first answer is not complete.
&
The second answer is correct.
<?php
$values = array("banana","apple","pear","banana");
if (in_array("pear",$values))
{
echo "Got pear";
}
?>
Is This Answer Correct ? | 6 Yes | 0 No |
Answer / jude jeevanraj.p
If you wish to check whether a value is already stored in
an array or not, then use the in_array function.
This is useful when you don't want any duplicates in the
array and therefore only want to add a value if it's not
already there. The first argument is the string you are
testing for and the second is the array you are checking
against.
Here is an example of in_array in action:
<?php
$values = array("banana","apple","pear","banana");
$newvalue = "pear";
if (in_array
Is This Answer Correct ? | 7 Yes | 2 No |
Answer / deep
The above answer is correct. I will just add a complete
example.
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os))
{
echo "Got Irix";
}
if (in_array("mac", $os))
{
echo "Got mac";
}
?>
Is This Answer Correct ? | 7 Yes | 2 No |
Tips to optimize the php script..... Suggestion for exception handling in php...
How to find out, how a user visited a particular page?
what is the difference between GET,POST and REQUEST in php
What is the php function that removes the last element of the array and returns it?
Who is the father of php?
How to remove the new line character from the end of a text line in php?
How to run the interactive php shell from the command line interface?
How can we access the data sent through the url with the post method?
Tell me how is it possible to propagate a session id?
How to set a page as a home page in a php based site?
How big is varchar max?
How can we set and destroy the cookie in php?