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

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(”mango”,”apple”,”orange”,”banana”);
$newvalue = “apple”;
if (in_array($newvalue,$values)) { echo “$newvalue is already in the array!”; }
?>

No related posts.

Leave a Reply

XHTML: You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>