You can use the PHP in_array() function to test whether a value exists in an array or not.
The in_array() function searches an array for a specific value.
Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.
Syntax
in_array(search, array, type)
Parameter Values
search: Required. Specifies the what to search for
array: Required. Specifies the array to search
type: Optional. If this parameter is set to TRUE, the in_array() function searches for the search-string and specific type in the array.
Let's take a look at an example to understand how it basically works
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of Testing Whether a Value Exists in PHP Array</title>
</head>
<body>
<?php
$zoo = array("Lion", "Elephant", "Tiger", "Zebra", "Rhino", "Bear");
if(in_array("Elephant", $zoo)){
echo "The elephant was found in the zoo.";
}
echo "<br>";
if(in_array("Tiger", $zoo)){
echo "The tiger was found in the zoo.";
}
?>
</body>
</html>
Output
The elephant was found in the zoo.
The tiger was found in the zoo.
No comments:
Post a Comment