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

Answers were Sorted based on User's Feedback



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

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

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

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

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

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

Post New Answer

More PHP Interview Questions

am from coimbatore.am learning php&mysql. what will be the approx salary for me if i get a job in coimbatore.

0 Answers   HCL,


what is PHP accelerator ?

2 Answers  


Is php free to use?

0 Answers  


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 ?

2 Answers   IPMC, NIIT,


What is null value in php?

0 Answers  






What is php limit?

0 Answers  


What is the best way to avoid email sent through php getting into the spam folder?

0 Answers  


What is namespace in php?

0 Answers  


What is the use of callback in php?

0 Answers  


Is php 5 still supported?

0 Answers  


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

0 Answers  


Explain Parsing and Execution in PHP? Where PHP Engine Return errors?

3 Answers   CMC,


Categories