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

What is super keyword in c++?

0 Answers  


when ever the user logged in the database table the current registered date will be appeared

3 Answers  


Which of the following represents the proper way to set a session variable? Answer: a. $_SESSION['foo'] = 'bar'; b. session_start(); c. session_set_save_handler ('myopen', 'myclose', 'myread', 'mywrite', 'mydelete', 'mygarbage'); d. $foo = $_SESSION['foo']; 2.When administering MySQL, you should make the data directory accessible via the operating system 3.which statement can be used to determine how the optimizer resolves a query

9 Answers   Cybercom, TCS,


Does winrunner 8.0 or qtp 8.0 supports php

2 Answers  


Write a program to upload a file in php?

0 Answers  






we store and display scores of users in different games. In MySQL, records are stored as tuples (user-id, game-id, score). Now we need to support ranks of users, i.e., each user should be informed of his current rank in the community. The challenge is to come up with the best way to store the data in MySQL so that the requirements are efficiently met.

1 Answers   IndiaTimes,


How to print \ in php. Only \ with out using . or *.

7 Answers   American Solutions,


how we can use lamp.ie installatiion and basic workings.Is it similar to wamp in usage?

1 Answers  


Explain the casts allowed in PHP?

0 Answers  


How to convert one date format into another in php?

0 Answers  


what is used for tmp table in oracle?

0 Answers  


Write a program in php to check whether a number is prime or not?

0 Answers  


Categories