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
Can you pass an array into a function?
Write a program using while loop in php?
What are differences between PECL and PEAR?
How to check a variable is array or not in php?
What is the purpose of pear in php?
What is difference between ksort() and usort() functions.
How to retrieve the original query string?
What is the use of Php variables?
Which functions are used to remove whitespaces from the string?
Where are cookies stored php?
What are the array functions in php?
What is stdclass in php?
What will the ?getdate() function returns in PHP?
Why do we use polymorphism in php?
What is in php 7?