How do I find if a value is already in an array or not?
It is strange how we tend not to need to use this function the first few times we use PHP, but then when occasions crop up that we need to use this function we find that there are many, many times where we need to know if a value is already in an array or not.
Here is how to use this simple function:
<?php
$values = array(1,20,10);
$masterarray = range(1,10);//contains 1 - 10
foreach($values as $test) {
if(in_array($test,$masterarray)) { echo "$test is in the masterarray\n";}
}
//prints:
//1 is in the masterarray
//10 is in the masterarray
?>
Ask a Question / Comment on this Question and Answer >>>
More arrays PHP Questions
How can I mix up the order of values in an array?How do I sort alphanumeric array data correctly?
I have an array of numbers and many repeat, how to list all items in order of maximum occurence?
How can I easily view all members of an array?
How can I create an array of the letters of the alphabet?
