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 |
am from coimbatore.am learning php&mysql. what will be the approx salary for me if i get a job in coimbatore.
what is PHP accelerator ?
Is php free to use?
Questions on OOP concepts 1. What are the access specifiers available in php ? Explain 2. What is object cloning ? 3. What are the differences between interface and abstract class ? 4. What is overloading ? 5. What is overriding ? 6. How to prevent function overriding ? 7. What is the use of "final" keyword ? 8. What is static variable ? How will access a static variable ? What is static class ?
What is null value in php?
What is php limit?
What is the best way to avoid email sent through php getting into the spam folder?
What is namespace in php?
What is the use of callback in php?
Is php 5 still supported?
I need to know about the courses which are useful in corporate companies.. especially php/mySQL, Java/j2ee, .NET.. also tell if any other courses are valuable
Explain Parsing and Execution in PHP? Where PHP Engine Return errors?