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 |
please explain me mail function in php
What are the security measures we have to take for our site not to hack by others?
How to retrieve values out of an array?
How to redirect https to http url and vice versa in .htaccess?
List out some tools through which we can draw E-R diagrams for mysql.
how set session expire time in php?
How to join multiple strings into a single string?
What are the environmental variables?
Explain do you use composer? If yes, what benefits have you found in it?
which institute provide better PHP trainning in delhi? please tell me how much salary can freshers get ? thanku
How to add comments in php?
hi, i have knowledge about PHP/MYSQL,i am fresher of M.Sc-IT 2009 pass out,any recruitment in PHP please let me know.my email id:nathiyasms@gmail.com