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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain me what are the 3 scope levels available in php and how would you define them?

705


What the use of var_dump()?

562


Are php variables global?

534


What is preg_match?

521


What is form validation in php?

505






Why do we use javascript in php?

508


How to test if a variable is an array?

541


Is php a strongly typed language?

545


Is php closing tag necessary?

531


Explain Type juggling?

548


What is the functionality of the functions strstr() and stristr()?

504


What is the use of header() function in php?

523


Which is useful for method overloading?

601


What is the functionality of md5 function in php?

568


What is a composer in PHP?

524