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 |
What are the basic steps to store PHP session in to Database?
To find out the current year is leap year or not which date() function we've to use.Give the syntax also
How to retrieve values out of an array?
How to set a page as a home page in a php based site?
What is new static in php?
What is $_ get in php?
What php framework does wordpress use?
How to generate a form?
How do I run a php program in dreamweaver?
What is the role of php.ini file?
How many ways can we get the value of current session id?
3 Answers Infosys, L&T, SysBiz, Torque Infotech,
How can we upload a file in php?