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

What is move_uploaded_file in php?

554


What are the file upload settings in configuration file?

538


Explain what are the three classes of errors that can occur in php?

528


What is meant by variable variables in php?

533


How does one prevent the following warning ‘warning: cannot modify header information – headers already sent' and why does it occur in the first place?

517






What is an array in php?

623


What is php namespace?

541


What is difference between isset and empty in php?

540


Is php a low level language?

538


How do I escape data before storing it into the database?

534


What are the two main string operators?

549


Which cryptographic extension provide generation and verification of digital signatures?

530


How do you sort an array in php?

533


Write an example to remove html tags from a string in php?

523


Is php a float?

543