The
array_search() array function in PHP will search in an array for a specific value. If found it will return the corresponding key for that value. It is a good way to check to see if a value exists in an array. Remember that all arrays have a default index of 0, so the first element has a key of 0 when key labels are not specified in the array.
<?php
$targetArray = array("Sara","Cindy","Julie","Megan");
$find = "Julie";
$keyFound = array_search($find, $targetArray);
if ($keyFound) {
echo "The key where $find was found is: $keyFound";
} else {
echo "$find was NOT FOUND in the array";
}
?>
The key where Julie was found is: 2